Created
March 28, 2014 16:33
-
-
Save bign8/9837080 to your computer and use it in GitHub Desktop.
Image Pre-loader
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
// Note: pre-fetching all images is dumb! It wastes bandwidth on content that "might" be viewed | |
// Instead: Look up something called "Lazy Loading" | |
(function (queue, mapper) { | |
mapper = mapper || function (obj) { return obj; }; | |
var image = new Image(), index = 0; | |
image.onload = function () { | |
if (index < queue.length) image.src = mapper( queue[index++] ); | |
}; | |
image.onerror = function (e) { | |
console.log(e); | |
image.onload(); | |
}; | |
image.onload(); | |
})( ['img/img1.png', 'img/img2.png', 'img/img3.png'], function (obj) { return obj; } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment