Skip to content

Instantly share code, notes, and snippets.

@ryanhanwu
Created August 28, 2012 05:02
Show Gist options
  • Save ryanhanwu/3495094 to your computer and use it in GitHub Desktop.
Save ryanhanwu/3495094 to your computer and use it in GitHub Desktop.
image preloader
$(function () {
var images = ['image1.jpg','image2.jpg' /* ... */ ];
var imageObjects = [];
var imagesToLoad = 0;
for (i = 0, z = images.length; i < 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 = $('<img />').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
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment