Created
January 10, 2012 08:56
-
-
Save brandon-lockaby/1587949 to your computer and use it in GitHub Desktop.
q&d js downloadImage & downloadImages
This file contains hidden or 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
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); | |
} | |
} | |
}); | |
})(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gisty