定义于:throttler.ts:71
一个创建节流函数的类。
节流确保函数在指定的时间窗口内最多调用一次。 与等待调用暂停的防抖不同,节流无论调用频率如何,都能保证一致的执行时序。
支持前沿和后沿执行:
对于只关心最后一次调用的快速连续事件的折叠,请考虑使用 Debouncer。
const throttler = new Throttler(
(id: string) => api.getData(id),
{ wait: 1000 } // 每秒最多执行一次
);
// 首次调用立即执行
throttler.maybeExecute('123');
// 1000 毫秒内的后续调用将被节流
throttler.maybeExecute('123'); // 已节流
const throttler = new Throttler(
(id: string) => api.getData(id),
{ wait: 1000 } // 每秒最多执行一次
);
// 首次调用立即执行
throttler.maybeExecute('123');
// 1000 毫秒内的后续调用将被节流
throttler.maybeExecute('123'); // 已节流
• TFn extends AnyFunction
new Throttler<TFn>(fn, initialOptions): Throttler<TFn>
new Throttler<TFn>(fn, initialOptions): Throttler<TFn>
定义于:throttler.ts:78
TFn
ThrottlerOptions<TFn>
Throttler<TFn>
cancel(): void
cancel(): void
定义于:throttler.ts:189
取消任何待处理的后沿执行并清除内部状态。
如果已计划后沿执行(由于使用 trailing=true 进行节流), 这将阻止该执行发生。内部超时和存储的参数将被清除。
如果没有待处理的执行,则无效。
void
getEnabled(): boolean
getEnabled(): boolean
定义于:throttler.ts:110
返回节流器的当前启用状态
boolean
getExecutionCount(): number
getExecutionCount(): number
定义于:throttler.ts:214
返回函数已执行的次数
number
getIsPending(): boolean
getIsPending(): boolean
定义于:throttler.ts:221
如果存在待处理的执行,则返回 true
boolean
getLastExecutionTime(): number
getLastExecutionTime(): number
定义于:throttler.ts:200
返回上次执行时间
number
getNextExecutionTime(): number
getNextExecutionTime(): number
定义于:throttler.ts:207
返回下次执行时间
number
getOptions(): Required<ThrottlerOptions<TFn>>
getOptions(): Required<ThrottlerOptions<TFn>>
定义于:throttler.ts:103
返回当前节流器选项
Required<ThrottlerOptions<TFn>>
getWait(): number
getWait(): number
定义于:throttler.ts:117
返回当前等待时间(以毫秒为单位)
number
maybeExecute(...args): void
maybeExecute(...args): void
定义于:throttler.ts:143
尝试执行节流函数。执行行为取决于节流器选项:
如果自上次执行以来已过足够的时间(>= 等待期):
如果在等待期内:
...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');
setOptions(newOptions): void
setOptions(newOptions): void
定义于:throttler.ts:91
更新节流器选项
Partial<ThrottlerOptions<TFn>>
void