Created
March 10, 2014 22:28
-
-
Save vitorjustin/9475834 to your computer and use it in GitHub Desktop.
simple image preloading
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
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