function throttle<TFn>(fn, initialOptions): (...args) => void
function throttle<TFn>(fn, initialOptions): (...args) => void
定义于:throttler.ts:252
创建一个节流函数,该函数限制所提供函数的执行频率。
节流确保函数在指定的时间窗口内最多执行一次, 无论它被调用多少次。这对于限制昂贵的操作或 UI 更新的速率非常有用。
可以将节流函数配置为通过选项在节流窗口的前沿和/或后沿执行。
对于处理突发事件,请考虑改用 debounce()。对于硬执行限制,请考虑改用 rateLimit()。
• TFn extends AnyFunction
TFn
ThrottlerOptions<TFn>
Function
尝试执行节流函数。执行行为取决于节流器选项:
如果自上次执行以来已过足够的时间(>= 等待期):
如果在等待期内:
...Parameters<TFn>
void
const throttled = new Throttler(fn, { wait: 1000 });
// 首次调用立即执行
throttled.maybeExecute('a', 'b');
// 等待期内的调用 - 被节流
throttled.maybeExecute('c', 'd');
const throttled = new Throttler(fn, { wait: 1000 });
// 首次调用立即执行
throttled.maybeExecute('a', 'b');
// 等待期内的调用 - 被节流
throttled.maybeExecute('c', 'd');
// 基本节流 - 每秒最多一次
const throttled = throttle(updateUI, { wait: 1000 });
// 配置前沿/后沿执行
const throttled = throttle(saveData, {
wait: 2000,
leading: true, // 首次调用时立即执行
trailing: true // 如果在等待期间调用,则在延迟后再次执行
});
// 基本节流 - 每秒最多一次
const throttled = throttle(updateUI, { wait: 1000 });
// 配置前沿/后沿执行
const throttled = throttle(saveData, {
wait: 2000,
leading: true, // 首次调用时立即执行
trailing: true // 如果在等待期间调用,则在延迟后再次执行
});