Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>FINAL ATTEMPT</h2> <p>As far as I can tell you want to be able to tell when the image loads, and do it multiple times. The problem with the previous attempt, and your own code, is that the "load" handler is only applied <strong>AFTER</strong> the new image source has been set and executed. Please, try this on for size:</p> <pre><code>$(function () { var images = ['image1.jpg','image2.jpg' /* ... */ ]; var imageObjects = []; var imagesToLoad = 0; for (i = 0, z = images.length; i &lt; z; i++) { imageObjects[i] = new Image(); imagesToLoad++; $(imageObjects[i]) .load(function () { if (--imagesToLoad == 0) { // ALL DONE! } // anything in this function will execute after the image loads var newImg = $('&lt;img /&gt;').attr('src',$(this).attr('src')); $('.profile').append( $(newImg) ); // I assume this is the code you wanted to execute }) .attr('src',images[i]); // Notice that the 'attr' function is executed AFTER the load handler is hooked } }); </code></pre> <h2>[OLD] NEW ATTEMPT</h2> <pre><code>var images = [ /* you get it by now */ ]; for (i=0,z=images.length;i&lt;z;i++) { $('&lt;img /&gt;') .attr('src', images[i]) .load(function(){ $('.profile').append( $(this) ); // Your other custom code }); } </code></pre> <h2>ORIGINAL POST</h2> <p>Idea taken from <a href="https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-5214317.html" rel="nofollow noreferrer">This Article</a></p> <pre><code>$(document).ready(function(){ var images = ["image1.jpg","image2.jpg"]; var loader = new Image(); for (i=0,z=images.count;i&lt;z;i++) { loader.src = images[i]; // insert some code here to do something with the now-loaded image } // do something with the now-loaded images. }); </code></pre> <p>Alternatively,</p> <pre><code>var images = ["image1.jpg","image2.jpg"]; var loader = []; $(document).ready(function(){ for (i=0,z=images.count;i&lt;z;i++) { loader[i] = new Image(); loader[i].src = images[i]; // insert some code here to do something with the now-loaded image } // do something with the now-loaded images, using the loader array }); </code></pre>
    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.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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