高阶函数--节流功能

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);
    };
  }

你可能感兴趣的:(高阶函数--节流功能)