Skip to content

Instantly share code, notes, and snippets.

@bign8
Created March 28, 2014 16:33
Show Gist options
  • Save bign8/9837080 to your computer and use it in GitHub Desktop.
Save bign8/9837080 to your computer and use it in GitHub Desktop.
Image Pre-loader
// 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