Skip to content

Instantly share code, notes, and snippets.

@vitorjustin
Created March 10, 2014 22:28
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 vitorjustin/9475834 to your computer and use it in GitHub Desktop.
Save vitorjustin/9475834 to your computer and use it in GitHub Desktop.
simple image preloading
var preloadImages = function(images, callback) {
var current = 0,
imageEvtHandler = function() {
if (current++ === images.length && typeof callback === "function")
callback.call(this);
};
if (images.length > 0) {
for (var i = images.length - 1; 0 <= i; i--) {
var img = new Image;
img.onload = imageEvtHandler;
img.onerror = imageEvtHandler;
img.src = images[i];
}
} else {
if (typeof callback === "function")
callback.call(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment