Skip to content

Instantly share code, notes, and snippets.

@benbarnett
Created November 30, 2012 16:08
Show Gist options
  • Save benbarnett/4176685 to your computer and use it in GitHub Desktop.
Save benbarnett/4176685 to your computer and use it in GitHub Desktop.
Preload multiple images
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