Last active
December 18, 2015 14:28
-
-
Save thealscott/5797009 to your computer and use it in GitHub Desktop.
Simple asset preloader example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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