Skip to content

Instantly share code, notes, and snippets.

@destan
Created March 23, 2016 15:27
Show Gist options
  • Save destan/9449f3f122eb8aa437ac to your computer and use it in GitHub Desktop.
Save destan/9449f3f122eb8aa437ac to your computer and use it in GitHub Desktop.
sequential image preloader
function preload(imageArray, imageCache, index) {
Array.isArray = typeof Array.isArray == 'function' ? Array.isArray : function(a){return typeof a != 'undefined' && a.constructor == Array}
index = index || 0;
if (imageArray && imageArray.length > index) {
var img = new Image ();
var preloadFn = preload.bind(this, imageArray, imageCache, index + 1);
img.onload = preloadFn;
img.onerror = preloadFn;
img.src = imageArray[index];
if(Array.isArray(imageCache)) {
imageCache.push(img);
}
}
}
// Based on http://www.photo-mark.com/notes/image-preloading/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment