页面有一组图片实现当一张图片加载完后显示下一张图片
实现代码
const images = ['https://img-blog.csdnimg.cn/20201014180756928.png', 'https://img-blog.csdnimg.cn/20201014180756928.png']
const loadImg = (url) => {
return new Promise(resolve => {
let image = new Image()
image.src = url
image.onload = () => {
resolve(image)
}
image.onerror = () => {
reject(`Errors on loading`)
}
})
}
const solve = (images) => {
const imgQueue = []
for (let i = 0; i < images.length; i++) {
imgQueue.push(loadImg(images[i]))
}
Promise.all(imgQueue).then((item) => {
console.log(item)
})
}
solve(images)
本文来自博客园,作者:JackieDYH,转载请注明原文链接:https://www.cnblogs.com/JackieDYH/p/17634057.html