Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can set a flag on the image object when it loads and test that flag later:</p> <pre><code>var fname = getImgFnameFromDate(datetime); var imgAdd = new Image(); imgAdd.onload = function() { this.myLoadFlag = true; } imgAdd.src = fname; imgStore.push(imgAdd); </code></pre> <p>Then elsewhere, you can check to see if the image has been loaded yet:</p> <pre><code>if (imgStore[imgIndex].myLoadFlag) { // image has been loaded } </code></pre> <p>When using an onload handler, just make sure that you set it before you set the <code>.src</code> property otherwise, there are some cases in some browsers where you will miss the onload event if the image is already in the browser cache.</p> <hr> <p>An alternate approach (perhaps simpler to use) would be to have a second array that only contains loaded images and your main code only uses that:</p> <pre><code>// place we first put images, they go into imgStore only when actually loaded var imgPending = []; var fname = getImgFnameFromDate(datetime); var imgAdd = new Image(); imgAdd.onload = function() { imgStore.push(imgAdd); // now that it's loaded, add it to the images we're using } imgAdd.src = fname; imgPending.push(imgAdd); </code></pre> <p>Then, your code that uses <code>imgStore</code> will only ever see images that are actually loaded. One downside to this is that images go into <code>imgStore</code> in load order, not necessarily in the order that you request them so the order this way could be different than your old way.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. COThanks for that. However there still appears to be a problem. After creating the image array with the onload() behavior as you suggest, I try looping over the images skipping those without the myLoadFlag=true. Unfortunately it skips all the images. It seems as though this check gets triggered before the image has had a chance to load (or something like that). I made sure I was setting the onload() before .src. By contrast, if I let it just display all the images but print out to screen the value of this flag, it all looks correct; missing images are undefined and good images are true.
      singulars
    2. CO<continuing> so it seems that when given a bit more of a chance the images do load, but using the suggested solution falsely decides the images have not loaded, before they have had a chance to do so. I'd really like to 'force' the images to load, rather than it happening whenever the browser feels like it, if that is possible?
      singulars
    3. CO@user1282133 - You can preload the images with javascript when the page first loads rather than trying to load them via JS later when you need them. That gives them more time to load before you need them. There's no way to make them load any faster. They take time to load. FYI, this code doesn't falsely decide the images haven't loaded. It tells you when they haven't loaded YET. You're just expecting them to be loaded before they are ready. Unless you go with a preload scheme, your code needs to WAIT until they have time to load.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload