Skip to content

Instantly share code, notes, and snippets.

@brandon-lockaby
Created January 10, 2012 08:56
Show Gist options
  • Save brandon-lockaby/1587949 to your computer and use it in GitHub Desktop.
Save brandon-lockaby/1587949 to your computer and use it in GitHub Desktop.
q&d js downloadImage & downloadImages
window.downloadImage = function(url, cb) {
var img = new Image();
img.onerror = function() {
cb("onerror", img);
};
img.onabort = function() {
cb("onabort", img);
};
img.onload = function() {
cb(false, img);
};
img.src = url;
};
window.downloadImages = function(urls, cb) {
var imgs = new Array(urls.length);
var c = 0;
for(var i in urls) {
(function() {
var j = i;
downloadImage(urls[j], function(err, img) {
if(err) {
cb(err, imgs);
cb = function() {};
} else {
imgs[j] = img;
if(++c == urls.length) {
cb(false, imgs);
}
}
});
})();
}
};
@brandon-lockaby
Copy link
Author

gisty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment