-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimageCache.js
57 lines (45 loc) · 1015 Bytes
/
imageCache.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
/**
* imageCache.js - image caching framework.
* Zoltan Hawryluk - http://www.useragentman.com/
* MIT License.
*/
var imageCache = new function () {
var me = this;
var cache = [];
var root = document.location.href.split('/');
root.pop();
root = root.join('/') + '/';
me.push = function (src, loadEvent) {
if (!src.match(/^http/)) {
src = root + src;
}
var item = new Image();
if (cache[src] && loadEvent) {
loadEvent(src);
} else {
if (loadEvent) {
item.onload = loadEvent;
item.onerror = loadEvent;
}
cache[src]=item;
}
item.src = src;
}
me.pushArray = function (array, imageLoadEvent, imagesLoadEvent) {
var numLoaded = 0;
var arrayLength = array.length;
for (var i=0; i<arrayLength; i++) {
me.push(array[i], function (e) {
if (imageLoadEvent) {
imageLoadEvent(e);
}
numLoaded++;
if (numLoaded == arrayLength) {
setTimeout(function () {
imagesLoadEvent(e);
}, 1)
}
})
}
}
}