Skip to content

Instantly share code, notes, and snippets.

@ratbeard
Created April 9, 2013 20:03
Show Gist options
  • Save ratbeard/5348901 to your computer and use it in GitHub Desktop.
Save ratbeard/5348901 to your computer and use it in GitHub Desktop.
///////////////////////
// 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