页面有一组图片实现当一张图片加载完后显示下一张图片

实现代码 

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)

posted @ 2022-11-05 20:54  JackieDYH  阅读(2)  评论(0编辑  收藏  举报  来源