-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (49 loc) · 1.06 KB
/
index.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
/**
* @version 0.0.1
*/
const loadImage = function (path, i, callback) {
var img = new Image();
if(callback) {
// 暂时成功失败都执行一样的
var cb = function() {
img.onload = null;
img.onerror = null;
callback(path, i);
img = null;
}
// TODO: 失败回调
img.onerror = function () {
// console.log(path);
cb(false);
}
img.onload = function () {
cb();
}
}
img.src = path;
}
const loadAllImages = function(imgs, callback, endCb) {
var len = imgs.length
, i
;
callback || (callback = function() {});
endCb || (endCb = function() {});
var count = imgs.length;
// this callback counts down the things to do.
var imgLoadedCb = function (path, idx) {
count --;
// console.log(len - count);
callback(path, len - count, len);
if (0 === count) {
endCb();
}
}
// 同步加载多张
for (i = 0; i < imgs.length; i++) {
// 'do' each thing, and await callback.
loadImage(imgs[i], i, imgLoadedCb);
}
}
export default {
loadAllImages
}