Created
November 30, 2012 16:08
-
-
Save benbarnett/4176685 to your computer and use it in GitHub Desktop.
Preload multiple images
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
function loadImgs(urls, doneCallback) { | |
var remaining = total = urls.length; | |
var imgs = []; | |
function imgLoad(url) { | |
if (!--remaining) { | |
doneCallback(imgs); | |
} | |
} | |
for (var i = urls.length - 1; i >= 0; i--) { | |
var url = urls[i]; | |
(function(url) { | |
var img = new Image(); | |
imgs.push(img); | |
img.onload = function() { | |
imgLoad.call(this, url); | |
}; | |
img.src = url; | |
})(url); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment