zoukankan      html  css  js  c++  java
  • promise 封装定时器

    function timeout(delay=1000){
      return new Promise((resolve,reject)=>{
        setTimeout(resolve,delay);
      })
    }

    timeout().then(()=>{
      console.log(1);
      return timeout(2000);
    }).then(()=>{
      console.log(2);
    });

    ============================================================================================================================================================================================================

    function interval(delay=1000,callback){
      return new Promise((resolve,reject)=>{
        let id = setInterval(()=>{
          callback(id,resolve);
        },delay)
      })
    }

    interval(100,(id,resolve)=>{
      let box = document.querySelector('#box');
      let left = parseInt(window.getComputedStyle(box).left)
      box.style.left = left + 10 + 'px';
      if(left>=100){
        clearInterval(id);
        resolve(box)
      }
    }).then(oNode=>{
      return interval(100,(id,resolve)=>{
        let width = parseInt(window.getComputedStyle(oNode).width);
        oNode.style.width = width-10 + 'px';
        if(width<=20){
          clearInterval(id);
          resolve(box);
        }
      })
    }).then(oNode=>{
      interval(100,(id,resolve)=>{
        let height = parseInt(window.getComputedStyle(oNode).height);
        oNode.style.height = height - 10 + 'px';
        if(height<=20){
          clearInterval(id);
        }
      })
    });

  • 相关阅读:
    iOSS--生成有logo的二维码
    inux网卡与MAC地址绑定方法总结
    Linux--多网卡的7种Bond模式
    Arp攻击实战
    5款免费Windows远程连接Linux桌面软件(VNC客户端)
    DenyHosts安装及配置
    Cacti完全使用手册 ( 让你快速个性化使用Cacti )
    解除被DenyHosts锁定的IP地址
    linux防止sshd被爆破(安装denyhosts)
    实现水电气一卡通 IC卡扇区分配
  • 原文地址:https://www.cnblogs.com/jayking1314/p/14903498.html
Copyright © 2011-2022 走看看