XingGuoZM/FE-blog

笔试题十四:预加载

Opened this issue · 0 comments

//预加载图片
const loadImage = (src)=>{
  return new Promise(resolve=>{
    const image = new Image();
    image.src=src;
    image.onload=()=>{
      resolve(true);
    }
  });
}

// 预加载文件(json)
const loadJson=(src)=>{
  return fetch(src).then(res=>res.json());
}

//批量预加载图片
const batchLoadImage = (list)=>{
  return Promise.all(list.map(src=>loadImage(src)))
}
// 批量预加载json
const batchJson = (list)=>{
  return Promise.all(list.map(src=>loadJson(src)));
}