Created
October 4, 2012 14:31
-
-
Save tamask/3833884 to your computer and use it in GitHub Desktop.
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
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