测试

91 阅读1分钟
复制代码
    function throttle(fn, delay) {
      let flag = true, // 加锁
          timer = null;
      return function (...args) {
        let context = this;
        if(!flag)return; // 如果还在固定频率内,不进行任何操作直接返回
        flag = false;
        clearTimeout(timer);
        timer = setTimeout(function () {
            fn.apply(context, args);
            flag = true;
        }, delay)
      }
    }