Skip to content

Instantly share code, notes, and snippets.

@mloberg
Created January 23, 2012 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloberg/1663355 to your computer and use it in GitHub Desktop.
Save mloberg/1663355 to your computer and use it in GitHub Desktop.
onImagesLoad
// MooTools
var onImagesLoad = function(callback){
var images = 0,
check;
$$("img").each(function(item, key){
images++;
var img = new Image();
img.onload = function(){
images--;
};
img.src = item.get("src");
});
check = setInterval(function(){
if(images === 0){
callback();
clearInterval(check);
return;
}
}, 50);
};
// jQuery
var onImagesLoad = function(callback){
var images = 0,
check;
$("img").each(function(key){
var item = $(this),
img = new Image();
images++;
img.onload = function(){
images--;
};
img.src = item.attr("src");
});
check = setInterval(function(){
if(images === 0){
callback();
clearInterval(check);
return;
}
}, 50);
};
// usage
onImagesLoad(function(){
// do something once all images are loaded
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment