Skip to content

Instantly share code, notes, and snippets.

@qawemlilo
Created January 27, 2011 13:53
Show Gist options
  • Save qawemlilo/798526 to your computer and use it in GitHub Desktop.
Save qawemlilo/798526 to your computer and use it in GitHub Desktop.
var oldPreload = {
myImages: [], // stores image url paths
counter: 0, // keeps count of all loaded images
intervalId: null, // keeps setInterval pointer id
imageFolder: [], // stores images objects
loaded: [], // keeps a boolean value for all images, true if loaded, false otherwise.
onComplete: function() {}, // method called when preloading is complete
startLoading: function () { // initialize preloading
var i, totalImages = this.myImages.length;
for (i = 0; i < totalImages; i += 1) { // loop thrugh all image urls
this.imageFolder[i] = new Image() // store image objects in temp folder
this.imageFolder[i].src = this.myImages[i]; // define src attribute
this.loaded[i] = false; // assume none has been loaded
}
this.intervalId = setInterval(this.checkLoad, 10); // check every 10 mlliseconds to see if images have loaded
},
checkLoad: function () {
var i, totalImages = oldPreload.myImages.length;
if (oldPreload.counter === totalImages) { // all images have been loaded
clearInterval(oldPreload.intervalId); // stop checking, clear intervals
oldPreload.onComplete(); // call the onComplete method
return; // exit
}
for (i = 0; i < totalImages; i += 1) { // loop through all image urls
if (!oldPreload.loaded[i] && oldPreload.imageFolder[i].complete) { // an has finished loading and is not yet marked
oldPreload.loaded[i] = true; // mark it as loaded
try { console.log(oldPreload.imageFolder[i].src + ' - loading complete');} // show result in firebug console
catch(e){}
oldPreload.counter += 1; // keep counting. one more image has been loaded
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment