高阶函数完成节流功能

280 阅读1分钟
let btnName = document.getElementById("btn");
  btnName.onclick = throttle(function () {
    console.log(1);
  }, 2000);
  function throttle(fn,wait) {
    let timer;
    return function (...args) {
      if (timer) return;
      timer = setTimeout(() => (timer = null), wait);
      return fn(args);
    };
  }