Skip to content

Instantly share code, notes, and snippets.

@tomhalley
Created March 30, 2015 14:59
Show Gist options
  • Save tomhalley/4f6310465e2b055bdc33 to your computer and use it in GitHub Desktop.
Save tomhalley/4f6310465e2b055bdc33 to your computer and use it in GitHub Desktop.
jQuery Deferred Image Preloader
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