zoukankan      html  css  js  c++  java
  • 防抖 和 节流

    防抖:动作绑定事件,动作发生后一定时间后触发事件,在这段时间内,如果该动作又发生,则重新等待一定时间再触发事件。

     function debounce(func, time) {
        let timer = null;
        return () => {
          clearTimeout(timer);
          timer = setTimeout(()=> {
            func.apply(this, arguments)
          }, time);
        }
      }


    实例: 定时轮询 如: 定时调状态接口

    节流: 动作绑定事件,动作发生后一段时间后触发事件,在这段时间内,如果动作又发生,则无视该动作,直到事件执行完后,才能重新触发。

      function throtte(func, time){
        let activeTime = 0;
        return () => {
          const current = Date.now();
          if(current - activeTime > time) {
            func.apply(this, arguments);
            activeTime = Date.now();
          }
        }
      }

    实例: 点击按钮调接口后  loading 不能被点击  等接口返回结果后 再点击 

     
    越努力越幸运
  • 相关阅读:
    继承
    JAVA接口的继承与集合
    JAVA接口
    c++程序—敲桌子
    c++程序—水仙花数
    c++程序—while猜数字游戏
    c++程序—switch分支
    c++程序—三目运算符
    c++程序—if语句实践
    c++程序—选择结构
  • 原文地址:https://www.cnblogs.com/guangzhou11/p/14447525.html
Copyright © 2011-2022 走看看