Skip to content

Instantly share code, notes, and snippets.

@thealscott
Last active December 18, 2015 14:28
Show Gist options
  • Save thealscott/5797009 to your computer and use it in GitHub Desktop.
Save thealscott/5797009 to your computer and use it in GitHub Desktop.
Simple asset preloader example
var preloadImageArray = [
'/path/to/images_1.jpg',
'/path/to/images_2.jpg',
'/path/to/images_3.jpg',
'/path/to/images_4.jpg'
];
function preloadImages (preloadImageArray) {
var images = preloadImageArray;
var count = images.length;
var counter = 0;
var loadCompleteHandler = function(){
//call the ajax for your page module loading here
}
$(images).each(function(i){
var image = new Image()
image.onload = function(){
counter++;
console.log(images[i]);
console.log(counter + " of " + count);
if (counter == count) {
console.log('Images Preloaded');
loadCompleteHandler();
}
}
image.src = this;
});
}
preloadImages(preloadImageArray);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment