• 图片异步加载的几种实现方案


    一. 异步串行加载

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

    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();
  • 相关阅读:
    App的开发过程(转载)
    一款APP的完整开发流程 (转载)
    JS判断是否是数组的四种做法(转载)
    easyui datagrid Column Group 列组、 复杂表头 嵌套表头 组合表头 (转载)
    php bootstrap-datetimepicker
    Echarts实现Excel趋势线和R平方计算思路
    前端项目开发流程(转载)
    对有序特征进行离散化(继承Spark的机器学习Estimator类)
    Spark ML 中 VectorIndexer, StringIndexer等用法(转载)
    Z-Score数据标准化(转载)
  • 原文地址:https://www.cnblogs.com/lyraLee/p/11806663.html
Copyright © 2020-2023  润新知