Created
January 26, 2013 06:50
-
-
Save zachstronaut/4640634 to your computer and use it in GitHub Desktop.
quick and dirty 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 = [ | |
'path/to/image1.png', | |
'path/to/image2.png', | |
'path/to/image3.png' | |
], | |
toLoad = 0, | |
preload = function () { | |
var i = preloadImages.length; | |
toLoad += i; | |
while (i--) { | |
setTimeout((function (i) { | |
return function () { | |
var img = new Image(); | |
img.onload = onload; | |
img.src = preloadImages[i]; | |
}; | |
}(i)), 1); | |
} | |
}, | |
onload = function () { | |
toLoad--; | |
if (toLoad === 0) { | |
// everything is loaded! | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment