downLoad

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
class DownLoad {
constructor(type) {
this.arr = {}
this.type = type
this.arr[type] = 0
}
int() {
this.arr[this.type] += 1
this.total()
}
total() {
let num = Object.entries(this.arr).reduce((all, [key, value]) => {
all += value
return all
}, 0);
console.log('总数量:', num)
return num
}
}
class DownLoadSmall {
constructor(res = { isDown: false, url: './1.png' }) {
this.arr = res
}
start(DownLoad) {
if (this.arr.isDown) return this.arr
return new Promise((resolve) => {
setTimeout(() => {
this.arr.isDown = true
DownLoad.int()
resolve(this.arr)
}, 500)
})
}
}
class Image {
constructor(images) {
this.images = images
}
startDown(DownLoad) {
return new Promise(async resolve => {
for (let i = 0; i < this.images.length; i++) {
let data = new DownLoadSmall(this.images[i])
this.images[i] = await data.start(DownLoad, this.images[i])
}
console.log(this.images)
resolve()
})
}
}

let images = [
{ isDown: false, url: './1.png' },
{ isDown: false, url: './2.png' },
{ isDown: false, url: './3.png' },
{ isDown: false, url: './4.png' },
{ isDown: false, url: './5.png' },
{ isDown: false, url: './6.png' },
{ isDown: false, url: './7.png' },
]
async function startDownImage() {
let imgClass = new DownLoad('image')
let img = new Image(images)
await img.startDown(imgClass)
await img.startDown(imgClass)
}
startDownImage()