预加载

静态资源形成队列,加载完毕执行

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
preload () {
let base, level, avatar, imgList, waitList
waitList = []
imgList = [base, level, avatar]
for (let index = 0; index < imgList.length; index++) {
const element = imgList[index];
element = document.createElement('img')
switch (index) {
case 0:
element.src = './static/level_base.png'
element.onload = () => {
waitList.push('ok1')
imgList[index] = element
if (waitList.length == 3) this.init(imgList)
}
break
case 1:
element.src = './static/level_core_5.png'
element.onload = () => {
waitList.push('ok2')
imgList[index] = element
if (waitList.length == 3) this.init(imgList)

}
break
case 2:
element.src = this.user.avatar
element.onload = () => {
waitList.push('ok3')
imgList[index] = element
if (waitList.length == 3) this.init(imgList)
}
break
}
}
},