function debounce(fn, delay) {
// 持久化一个定时器 timer
    let timer = null;
    // 闭包函数可以访问 timer
    return function() {
    // 通过 'this' 和 'arguments'
    // 获得函数的作用域和参数
    let context = this;
    let args = arguments;
    // 如果事件被触发,清除 timer 并重新开始计时
    clearTimeout(timer);
    timer = setTimeout(function() {
    fn.apply(context, args);
    }, delay);
    }
}

 

相关文章:

  • 2021-11-03
  • 2022-01-26
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2021-11-03
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-03
  • 2021-09-05
  • 2021-12-08
  • 2021-11-12
相关资源
相似解决方案