zoukankan      html  css  js  c++  java
  • 图片异步加载的几种实现方案

    一. 异步串行加载

    要求图片依次加载(上一张加载完成,再加载下一张)

    const imgs = [url1, url2, url3,...];

    1. ES5--递归方法

    function loadImage() {
      const image = new Image();
      const itemUrlImage = imgs.shift();
      if (itemUrlImage) {
        image.src = itemUrlImage;  
        image.onload = function() {
          document.body.appendChild(image);
          loadImage();
        }
      }   
    }
    loadImage(); 

    2. ES6--Promise对象

     function loadByPromise() {
      let promise = function(url) {
        return new Promise((resolve, reject) => {
          const image = new Image();
          image.src = url;
          image.onload = function() {
            document.body.appendChild(image);
            resolve('ok');
          }        
        })      
      }
      let p = Promise.resolve();
      for(let url of imgs) {
        p = p.then(() => promise(url))
      }
      p.catch(() => {}).then(() => {
        console.log("加载完成");
      })
    }
    loadByPromise();

    3.ES6--async函数

    async function loadAsync() {
      let promise = function(url) {
        return new Promise((resolve, reject) => {
          const image = new Image();
          image.src = url;
          image.onload = function() {
            document.body.appendChild(image);
            resolve('ok');
          }        
        })      
      }
      for(let url of imgs) {
        await promise(url);
      }
    }
    loadAsync();

     二. 异步并行加载

    要求图片并行加载,全部加载完成后,再插入DOM。

    const imgs = [url1, url2, url3,...];

    1. async函数

    async function loadAsync() {
      let promise = function(url) {
        return new Promise((resolve, reject) => {
          const image = new Image();
          image.src = url;
          image.onload = function() {
            resolve(image);
          }        
        })      
      }
      const promises = imgs.map(i => promise(i)); //实现并行加载
      let results = [];
      for(let promise of promises) {
        results.push(await promise);  //等待加载完成
      }
      results.forEach(image => {
        document.body.appendChild(image);
      })
    }
    loadAsync();

    2.async函数+Promise.all

     async function loadAsync() {
      let promise = function(url) {
        return new Promise((resolve, reject) => {
          const image = new Image();
          image.src = url;
          image.onload = function() {
            resolve(image);
          }        
        })      
      }
      const promises = imgs.map(i => promise(i)); //实现并行加载
      const results = await Promise.all(promises);
      results.forEach(image => {
        document.body.appendChild(image)
      });
    }
    loadAsync();
  • 相关阅读:
    P2604 [ZJOI2010]网络扩容
    P2053 [SCOI2007]修车
    P2045 方格取数加强版
    P4134 [BJOI2012]连连看
    P2153 [SDOI2009]晨跑
    P3381 【模板】最小费用最大流
    P3376 【模板】网络最大流
    P1326 足球
    2020牛客多校第八场I题 Interesting Computer Game(并查集+判环)
    Codeforces 1375D Replace by MEX(思维题)
  • 原文地址:https://www.cnblogs.com/lyraLee/p/11806663.html
Copyright © 2011-2022 走看看