Created
April 9, 2013 20:03
-
-
Save ratbeard/5348901 to your computer and use it in GitHub Desktop.
This file contains 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
/////////////////////// | |
// Image Pre- Loading | |
/////////////////////// | |
// Use a timeout so failing to preload image doesn't stop the page from loading | |
// Can pass 'false' for timeout to never timeout | |
function loadImages (images, callback, timeout) { | |
var img, i, loaded = 0, total = images.length, called = false | |
//console.log('loading images', images.length, images) | |
if (timeout) setTimeout(callCallback, timeout) | |
for (i=0 ; i < total; i++) { | |
img = new Image | |
img.onload = onload | |
img.src = images[i] | |
} | |
function onload () { | |
loaded += 1 | |
console.log('loaded image %o of %o, %o', loaded, total, this.src) | |
if (loaded === total) callCallback() | |
} | |
function callCallback() { | |
console.log('done loading images', images) | |
if (!called) callback() | |
called = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment