定义于: throttler.ts:137
一个创建节流函数的类。
节流确保函数在指定的事件窗口内最多被调用一次。与防抖(debounce)不同,防抖会等待调用暂停,节流则保证了无论调用频率如何,执行时间都是一致的。
支持首尾触发执行
对于需要折叠快速触发事件且你只关心最后一次调用的情况,请考虑使用 Debouncer。
状态管理
const throttler = new Throttler(
(id: string) => api.getData(id),
{ wait: 1000 } // Execute at most once per second
);
// First call executes immediately
throttler.maybeExecute('123');
// Subsequent calls within 1000ms are throttled
throttler.maybeExecute('123'); // Throttled
const throttler = new Throttler(
(id: string) => api.getData(id),
{ wait: 1000 } // Execute at most once per second
);
// First call executes immediately
throttler.maybeExecute('123');
// Subsequent calls within 1000ms are throttled
throttler.maybeExecute('123'); // Throttled
• TFn extends AnyFunction
new Throttler<TFn>(fn, initialOptions): Throttler<TFn>
new Throttler<TFn>(fn, initialOptions): Throttler<TFn>
定义于: throttler.ts:145
TFn
ThrottlerOptions<TFn>
Throttler<TFn>
fn: TFn;
fn: TFn;
定义于: throttler.ts:146
key: undefined | string;
key: undefined | string;
定义于: throttler.ts:141
options: ThrottlerOptions<TFn>;
options: ThrottlerOptions<TFn>;
定义于: throttler.ts:142
readonly store: Store<Readonly<ThrottlerState<TFn>>>;
readonly store: Store<Readonly<ThrottlerState<TFn>>>;
定义于: throttler.ts:138
cancel(): void
cancel(): void
定义于: throttler.ts:305
取消任何挂起的尾触发执行并清除内部状态。
如果安排了尾触发执行(由于设置了 trailing=true 进行节流),这将阻止该执行发生。内部的超时和存储的参数将被清除。
如果没有挂起的执行,则没有效果。
void
flush(): void
flush(): void
定义于: throttler.ts:283
立即处理当前挂起的执行
void
maybeExecute(...args): void
maybeExecute(...args): void
定义于: throttler.ts:224
尝试执行节流函数。执行行为取决于节流器选项
如果自上次执行以来已经过去了足够的时间(>=等待周期)
在等待周期内
...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');
reset(): void
reset(): void
定义于: throttler.ts:316
将节流器的状态重置为其默认值
void
setOptions(newOptions): void
setOptions(newOptions): void
定义于: throttler.ts:166
更新节流器选项
Partial<ThrottlerOptions<TFn>>
void
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。