Skip to content

实现 throttle(节流)

功能:在一定时间间隔内多次触发,只会真正执行一次函数调用。

思路

  • 使用闭包保存上一次执行时间 lastTime
  • 返回一个新的函数,每次触发时:
    • 获取当前时间 now
    • 如果 now - lastTime >= delay,说明距离上次执行已经超过设定的间隔:
      • 调用原始函数 func.apply(this, args)
      • 更新 lastTime = now
  • 这种实现是「时间戳版」节流:第一次触发会立刻执行。

代码

js
/**
 * throttle 节流:一定时间内重复触发只执行一次
 * @param {function} func
 * @param {number} delay
 */

function throttle(func, delay) {
    let lastTime = 0;
    return function(...args) {
        let now = Date.now();
        // 第一次触发,或距离上次触发已经超过一定时间
        if (now - lastTime >= delay) {
            func.apply(this, args);
            lastTime = now;
        }
    }
}

// 创建节流函数 2000ms
const throttled = throttle(() => console.log("触发时间:", Date.now()), 2000);

// 模拟高频触发 100ms
setInterval(() => {
    throttled();
}, 100);