手写一个节流方法

35 阅读1分钟
const throttle = (func, delay) => {
  let lastCalledTime = 0;
  return (...args) => {
    const currentTime = Date.now();
    if (currentTime - lastCalledTime >= delay) {
      func(...args);
      lastCalledTime = currentTime;
    }
  };
};
 const handleScroll = () => {
   console.log("Scroll handled");
};
const throttledHandleScroll = throttle(handleScroll, 1000); // 1000毫秒间隔
window.addEventListener('scroll', throttledHandleScroll);