Last active
October 1, 2016 19:58
modified JavaScript image preloader with callback
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
/*! | |
* modified JavaScript image preloader with callback | |
* gist.github.com/eikes/3925183 | |
* gist.github.com/englishextra/bcaca065f3fbd6d29841f9e970dcdb73 | |
* jsfiddle.net/englishextra/r6rf51mL/ | |
* @param {String|object} a string/array of paths | |
* @param {Object} [cb] callback function | |
* preloadImages(["//farm2.staticflickr.com/1698/24651691653_41661b1f59_z.jpg", | |
* "//farm2.staticflickr.com/1575/24651691753_40fc6fdc5e_z.jpg"], | |
* function(a){for(var j=0,l=a.length;j<l;j+=1){alert(a[j].src);};}); | |
*/ | |
var preloadImages = function (a, cb) { | |
var f = 0, | |
m = []; | |
a = "[object Array]" === Object.prototype.toString.apply(a) ? a : [a]; | |
for (var g = function () { | |
f += 1; | |
f === a.length && cb && "function" === typeof cb && cb(m) | |
}, b = 0, l = a.length; b < l; b += 1) { | |
m[b] = new Image, | |
m[b].onabort = g, | |
m[b].onerror = g, | |
m[b].onload = g, | |
m[b].src = a[b]; | |
}; | |
}; | |
preloadImages(["//farm2.staticflickr.com/1698/24651691653_41661b1f59_z.jpg","//farm2.staticflickr.com/1575/24651691753_40fc6fdc5e_z.jpg"], function( a ) { | |
for (var j = 0, l = a.length; j < l; j += 1) { | |
alert(a[j].src); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment