Skip to content

Instantly share code, notes, and snippets.

@tamask
Created October 4, 2012 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamask/3833884 to your computer and use it in GitHub Desktop.
Save tamask/3833884 to your computer and use it in GitHub Desktop.
var Preloader = function(sources, oncomplete, onload) {
this.init(sources, oncomplete, onload);
}
Preloader.prototype = {
init: function(sources, oncomplete, onload) {
this.sources = sources;
this.oncomplete = oncomplete
this.onload = onload;
this.images = [];
this.counter = 0;
},
load: function() {
var self = this;
this.images = [];
this.counter = 0;
for (var i = 0; i < this.sources.length; i++) {
var im = new Image();
im.onload = function() {
self.counter++;
if (self.onload)
self.onload(this.src);
if (self.counter == self.images.length)
self.oncomplete(self.sources);
};
im.src = this.sources[i];
self.images.push(im);
}
}
};
function preload(images, complete, load) {
var loader = new Preloader(images, complete, load);
loader.load();
return loader;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment