SHOW:
|
|
- or go back to the newest paste.
1 | - | function imagePreloader(list, onload, each, error) |
1 | + | function imagePreloader(list, onload, each, onerror) |
2 | { | |
3 | this.list = list || null; | |
4 | this.onload = onload || null; | |
5 | this.each = each || null; | |
6 | - | this.error = error || null; |
6 | + | this.onerror = onerror || null; |
7 | this.ready = | |
8 | this.ignoreErrors = | |
9 | this.aborted = false; | |
10 | this.loaded = 0; | |
11 | this.quantity = null; | |
12 | }; | |
13 | ||
14 | imagePreloader.prototype.load = function() | |
15 | { | |
16 | var self = this; | |
17 | this.quantity = 0; | |
18 | this.ready = false; | |
19 | //for(var i in this.list) this.quantity++; | |
20 | ||
21 | for(var i in this.list) | |
22 | { | |
23 | if(typeof this.list[i] != "string") continue; | |
24 | this.quantity++; | |
25 | ||
26 | var uri = this.list[i]; | |
27 | this.list[i] = new Image(); | |
28 | this.list[i].nome = i; | |
29 | this.list[i].addEventListener("load", function() | |
30 | { | |
31 | self.loaded++; | |
32 | if(self.aborted) return; | |
33 | var nome = this.nome; | |
34 | delete this.nome; | |
35 | ||
36 | if(self.loaded == self.quantity) | |
37 | { | |
38 | self.ready = true; | |
39 | typeof self.each == "function" && self.each(this, nome); | |
40 | typeof self.onload == "function" && self.onload(); | |
41 | } | |
42 | else typeof self.each == "function" && self.each(this, nome); | |
43 | }, false); | |
44 | this.list[i].addEventListener("errorr", function() | |
45 | { | |
46 | if(self.ignoreErrors) this.dispatchEvent(new Event("load")); | |
47 | else | |
48 | { | |
49 | - | typeof self.error == "function" && self.error(this); |
49 | + | typeof self.onerror == "function" && self.onerror(this); |
50 | self.aborted = true; | |
51 | } | |
52 | }, false); | |
53 | this.list[i].src = uri; | |
54 | } | |
55 | ||
56 | return this; | |
57 | }; |