-
-
Save larodiel/96689f8e279bd99d0a13 to your computer and use it in GitHub Desktop.
Uses jQuery's new Deferred object to help with image loading
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 preloadImages = function(srcs) { | |
var dfd, i, img, l, p, promises; | |
jQuery.whenArray = function(array) { | |
return jQuery.when.apply(this, array); | |
}; | |
dfd = $.Deferred(); | |
promises = []; | |
img; | |
l; | |
p; | |
if (!$.isArray(srcs)) { | |
srcs = [srcs]; | |
} | |
l = srcs.length; | |
i = 0; | |
while (i < l) { | |
p = $.Deferred(); | |
img = $('<img />'); | |
img.load(p.resolve); | |
img.error(p.resolve); | |
promises.push(p); | |
img.get(0).src = srcs[i].src; | |
++i; | |
} | |
$.whenArray(promises).done(dfd.resolve); | |
return dfd.promise(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment