Skip to content

Instantly share code, notes, and snippets.

@hitautodestruct
Created November 11, 2014 12:34
Show Gist options
  • Save hitautodestruct/317984e09d11bf34dee2 to your computer and use it in GitHub Desktop.
Save hitautodestruct/317984e09d11bf34dee2 to your computer and use it in GitHub Desktop.
A javascript image preloader. Accepts an array of image paths and then initiates a callback when done loading.
// Accepts array of image paths relative to the current page.
var preLoadImages = function( srcs, callback ) {
if ( srcs.length ) {
var cache = [],
args_len = srcs.length;
for (var i = args_len; i--;) {
var cacheImage = document.createElement('img');
cacheImage.src = srcs[i];
cacheImage.onload = callback;
cache.push(cacheImage);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment