Skip to content

Instantly share code, notes, and snippets.

@seektan
Last active September 6, 2015 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seektan/3882d698ba37ed524744 to your computer and use it in GitHub Desktop.
Save seektan/3882d698ba37ed524744 to your computer and use it in GitHub Desktop.
simple img preload
var loadImg = function(src, handle_once, handle_done) {
var _len = src.length ;
var _load = function( i ) {
if (i >= _len) {
handle_done();
return ;
}
var img = new Image();
img.src = src[i];
img.addEventListener('load', function () {
i++;
handle_once(Math.ceil(100 * i / _len));
_load(i);
img = null;
}, false);
}
_load(0);
};
var loadImgCustom = function (imgs, prefix, cb, cbAll, debug) {
prefix = !!debug ? 'images/' : prefix;
for (var i = 0, k = null; k = imgs[i] ; i++ ) {
imgs[i] = prefix + k;
}
loadImg(imgs, function (n) {
cb(n);
}, function () {
cbAll();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment