Created
March 23, 2016 15:27
-
-
Save destan/9449f3f122eb8aa437ac to your computer and use it in GitHub Desktop.
sequential image preloader
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
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