-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreloader.js
66 lines (54 loc) · 1.52 KB
/
preloader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// http://www.webreference.com/programming/javascript/gr/column3/
function ImagePreloader(images, callback)
{
// store the callback
this.callback = callback;
// initialize internal state.
this.nLoaded = 0;
this.nProcessed = 0;
this.aImages = new Array;
// record the number of images.
this.nImages = images.length;
// for each image, call preload()
for ( var i = 0; i < images.length; i++ )
this.preload(images[i]);
}
ImagePreloader.prototype.preload = function(image)
{
// create new Image object and add to array
var oImage = new Image;
this.aImages.push(oImage);
// set up event handlers for the Image object
oImage.onload = ImagePreloader.prototype.onload;
oImage.onerror = ImagePreloader.prototype.onerror;
oImage.onabort = ImagePreloader.prototype.onabort;
// assign pointer back to this.
oImage.oImagePreloader = this;
oImage.bLoaded = false;
// assign the .src property of the Image object
oImage.src = image;
}
ImagePreloader.prototype.onComplete = function()
{
this.nProcessed++;
if ( this.nProcessed == this.nImages )
{
this.callback(this.aImages, this.nLoaded);
}
}
ImagePreloader.prototype.onload = function()
{
this.bLoaded = true;
this.oImagePreloader.nLoaded++;
this.oImagePreloader.onComplete();
}
ImagePreloader.prototype.onerror = function()
{
this.bError = true;
this.oImagePreloader.onComplete();
}
ImagePreloader.prototype.onabort = function()
{
this.bAbort = true;
this.oImagePreloader.onComplete();
}