I want a function to run when specific images are loaded, but I don't know how to wait for both to load before running. I only know how to chain them, like below:
Image1 = new Image(); Image1.src = 'image1-link.jpg';
Image2 = new Image(); Image2.src = 'image2-link.jpg';
Actually recently blogged a snippet of code/mini plugin that basically does this.
Here is the code:
/* Check if all images are loaded - Callback occurs when all images are loaded - image load errors are ignored (complete will be true) - Use: $('.wrap img').imagesLoaded(function(){ alert('all images loaded'); }); */
jQuery.fn.extend({ imagesLoaded: function( callback ) { var i, c = true, t = this, l = t.length; for ( i = 0; i < l; i++ ) { if (this[i].tagName === "IMG") { c = (c && this[i].complete && this[i].height !== 0); } } if (c) { if (typeof callback === "function") { callback(); } } else { setTimeout(function(){ jQuery(t).imagesLoaded( callback ); }, 200); } } });
If you use $(window).load(){ /* code here */ }); then you already know all images are loaded. But if you lazy load or add more images after the page has loaded, this is an alternative method.
Here is how to use it to run the callback after multiple images have finished loading:
Thanks, but not quite what I needed. I should have been more specific. These images aren't been loaded into tags, I'm using them in an HTML5 canvas. The user is able to change what's drawn on the canvas via buttons and I want to make sure the images related to that are all loaded up before running all my other code.
// loader will 'load' items by calling thingToDo for each item, // before calling allDone when all the things to do have been done. function loader(items, thingToDo, allDone) { if (!items) { // nothing to do. return; }
if ("undefined" === items.length) { // convert single item to array. items = [items]; }
var count = items.length;
// this callback counts down the things to do. var thingToDoCompleted = function (items, i) { count--; if (0 == count) { allDone(items); } };
for (var i = 0; i < items.length; i++) { // 'do' each thing, and await callback. thingToDo(items, i, thingToDoCompleted); } }
function loadImage(items, i, onComplete) { var onLoad = function (e) { e.target.removeEventListener("load", onLoad);
// this next line can be removed. // only here to prove the image was loaded. document.body.appendChild(e.target);
// notify that we're done. onComplete(items, i); } var img = new Image(); img.addEventListener("load", onLoad, false); img.src = items[i]; }
The downside to this is it has to wait till Image1 completely loads before getting the second. I want to try something like this:
How can I do this?
Actually recently blogged a snippet of code/mini plugin that basically does this.
Here is the code:If you use
$(window).load(){ /* code here */ });then you already know all images are loaded. But if you lazy load or add more images after the page has loaded, this is an alternative method.Here is how to use it to run the callback after multiple images have finished loading:Or, if you only have one image, just target that imageIf there are no images in your selector, it automatically runs the callback by default.
Thanks, but not quite what I needed. I should have been more specific. These images aren't been loaded into
and here is a jsFiddle link for you http://jsfiddle.net/8baGb/1/
Second solution:
And here is a link to the Stack Overflow thread