Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
quick and dirty preloading
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