首页 代码干货

前言

大多业务场景下都有一个Loading Page,有很多不同的写法,当然我分享的是捕获所有图片并监听load状态

let AllImg = document.querySelectorAll("img");
const promiseAll = [];
let img = [];
for (let i = 0; i < AllImg.length; i++) {
  const element = AllImg.item(i).src;
  promiseAll.push(new Promise((resolve, reject) => {
    img[i] = new Image()
    img[i].src = element
    img[i].onload = function () {
      resolve(img[i])
    }
  }))
}
Promise.all(promiseAll).then(() => {
  loadingInstance.close();
})

文章评论