Created
March 30, 2015 14:59
-
-
Save tomhalley/4f6310465e2b055bdc33 to your computer and use it in GitHub Desktop.
jQuery Deferred Image Preloader
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 images = [ | |
"pins/bluePin.png", | |
"pins/greenPin.png", | |
"pins/orangePin.png", | |
"pins/pinkPin.png", | |
"pins/purplePin.png", | |
"pins/yellowPin.png" | |
]; | |
var preloadImages = function() { | |
var output = []; | |
for(var i = 0; i < images.length; i++) { | |
var deferred = $.Deferred(); | |
var imgObj = new Image(); | |
imgObj.src = "image_path/" + images[i]; | |
imgObj.onload = deferred.resolve; | |
output.push(deferred.promise()); | |
} | |
return output; | |
}; | |
$.when.apply($, preloadImages()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment