原文链接:https://www.3mooc.com/front/articleinfo/281
可以采用es6中的Promise.all()来实现
let mulitImg = [
'http://www.3mooc.com/wp-content/uploads/2019/5/1.jpg',
'http://www.3mooc.com/wp-content/uploads/2019/5/2.jpg',
'http://www.3mooc.com/wp-content/uploads/2019/5/3.jpg',
'http://www.3mooc.com/wp-content/uploads/2019/5/4.jpg'
];
let promiseAll = [], img = [], imgTotal = mulitImg.length;
for(let i = 0 ; i < imgTotal ; i++){
promiseAll[i] = new Promise((resolve, reject)=>{
img[i] = new Image()
img[i].src = mulitImg[i]
img[i].onload = function(){
//第i张加载完成
resolve(img[i])
}
})
}
Promise.all(promiseAll).then((img)=>{
//全部加载完成
})
网友评论