-1

Desired to have a JavaScript to loop trough a specific URL to find existing images using number incrementation ("https://6od.hu/data/hej_Page_%D.png", where %D is incremented).

Trying to Check if image exists without loading it and using the while loop:

The working code

function imageExists(url) {
  return new Promise((resolve, reject) => {
    const img = new Image(url);
    img.onerror = reject;
    img.onload = resolve;
    const timer = setInterval(() => {
      if (img.naturalWidth && img.naturalHeight) {
        img.src = ''; /* stop loading */
        clearInterval(timer);
        resolve();
      }
    }, 10);
    img.src = url;
  });
}

function loadImg(i) {
  const src_pattern = "https://6od.hu/data/hej_Page_0%D.png"
  let src = src_pattern.replace("%D", i);
  imageExists(src)
    .then(() => console.log("Image exists."))
    .catch(() => console.log("Image not exists."));
}
console.log(loadImg(5)); // true|false needed to return here

How this Promise object can return true/false for the loop?

I am just learning these Promise objects, please try to be descriptive. Thank you.

Desired loop
let i = 1;
while (loadImg(i)) {
  console.log(i + ": " + loadImg(i)); // log all of the existing img than stop
  i++;
}
eapo
  • 1,053
  • 1
  • 19
  • 40
  • 1
    The `` must be loaded on the actual DOM in order for `.naturalWidth/Height` to return a value other than 0. – zer00ne Jan 04 '23 at 10:08
  • @zer00ne it's enough to preload the image to have it in cache to retrieve the `.naturalWidth/Height`, `.width/height`. Tested here: https://jsfiddle.net/2e0cnswz/1/ – eapo Jan 05 '23 at 02:52
  • Would be nice to know the reason of the down-wote to correct it :| – eapo Jan 05 '23 at 02:59

1 Answers1

1

I added missing return and changed then and catch handlers

Also, removed setInterval

Check one:

function imageExists(url) {
  return new Promise((resolve, reject) => {
    const img = new Image(url);
    img.onerror = reject;
    img.onload = resolve;
    img.src = url;
  });
}

function loadImg(i) {
  const src_pattern = "https://6od.hu/data/hej_Page_0%D.png"
  let src = src_pattern.replace("%D", i);
  return imageExists(src)
    .then(() => true)
    .catch(() => false);
}

(async() => {
  console.log(await loadImg(5));
})()

Loop:

function imageExists(url) {
  return new Promise((resolve, reject) => {
    const img = new Image(url);
    img.onerror = reject;
    img.onload = resolve;
    img.src = url;
  });
}

function loadImg(i) {
  const src_pattern = "https://6od.hu/data/hej_Page_0%D.png"
  let src = src_pattern.replace("%D", i);
  return imageExists(src)
    .then(() => true)
    .catch(() => false);
}

(async() => {
  for (let i = 1; true; i += 1) {
    const exists = await loadImg(i)
    if (!exists) break
    console.log(i, exists);
  }
})()
Konrad
  • 21,590
  • 4
  • 28
  • 64
  • 1
    Amazing simplicity. Thank you! Here is the final code: [Load all images from URL using integer increment](https://codepen.io/eaposztrof/pen/WNKGqvM?editors=0011) – eapo Jan 05 '23 at 07:34