当前位置:网站首页>简单理解防抖函数

简单理解防抖函数

2022-06-12 19:31:00 Hello^_^Monkey

  //防抖:用户触发事件过于频繁,只需要最后一次事件的操作
  const inp = document.querySelector("input");

  //封装一个防抖函数
  inp.oninput = debounce(function () {
    console.log(this.value);
  }, 500);
  function debounce(fn, delay) {
    let t = null;
    return function () {
      if (t !== null) {
        clearInterval(t);
      }
      t = setTimeout(() => {
        fn.call(this);
      }, delay);
    };
  }
原网站

版权声明
本文为[Hello^_^Monkey]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_48010432/article/details/125212638