function throttle<TFn>(fn, initialOptions): (...args) => void
function throttle<TFn>(fn, initialOptions): (...args) => void
定义于:throttler.ts:355
创建一个节流函数,限制提供函数的执行频率。
节流确保一个函数在指定的窗口时间内最多执行一次,无论它被调用多少次。这对于限制昂贵操作或 UI 更新的速率非常有用。
可以通过选项配置节流函数在节流窗口的前沿和/或后沿执行。
要处理事件爆发,请考虑使用 debounce()。要进行硬执行限制,请考虑使用 rateLimit()。
状态管理
• TFn extends AnyFunction
TFn
ThrottlerOptions<TFn>
Function
尝试执行节流函数。执行行为取决于节流器选项
如果自上次执行以来已经过去了足够的时间(>=等待周期)
在等待周期内
...Parameters<TFn>
void
const throttled = new Throttler(fn, { wait: 1000 });
// First call executes immediately
throttled.maybeExecute('a', 'b');
// Call during wait period - gets throttled
throttled.maybeExecute('c', 'd');
const throttled = new Throttler(fn, { wait: 1000 });
// First call executes immediately
throttled.maybeExecute('a', 'b');
// Call during wait period - gets throttled
throttled.maybeExecute('c', 'd');
// Basic throttling - max once per second
const throttled = throttle(updateUI, { wait: 1000 });
// Configure leading/trailing execution
const throttled = throttle(saveData, {
wait: 2000,
leading: true, // Execute immediately on first call
trailing: true // Execute again after delay if called during wait
});
// Basic throttling - max once per second
const throttled = throttle(updateUI, { wait: 1000 });
// Configure leading/trailing execution
const throttled = throttle(saveData, {
wait: 2000,
leading: true, // Execute immediately on first call
trailing: true // Execute again after delay if called during wait
});
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。