框架
版本
防抖器 API 参考
节流器 API 参考
速率限制器 API 参考
队列 API 参考
批处理器 API 参考

节流器

类: Throttler<TFn>

定义于: throttler.ts:137

一个创建节流函数的类。

节流确保函数在指定的事件窗口内最多被调用一次。与防抖(debounce)不同,防抖会等待调用暂停,节流则保证了无论调用频率如何,执行时间都是一致的。

支持首尾触发执行

  • 首触发(Leading):首次调用时立即执行(默认:true)
  • 尾触发(Trailing):如果在节流期间有调用,则在等待期结束后执行(默认:true)

对于需要折叠快速触发事件且你只关心最后一次调用的情况,请考虑使用 Debouncer。

状态管理

  • 使用 TanStack Store 进行响应式状态管理
  • 使用 initialState 来为创建节流器提供初始状态值
  • 使用 onExecute 回调来响应函数执行并实现自定义逻辑
  • 状态包括执行次数、上次执行时间、挂起状态等
  • 当直接使用类时,状态可以通过 throttler.store.state 访问
  • 当使用框架适配器(React/Solid)时,状态可以从 throttler.state 访问

示例

ts
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()

ts
new Throttler<TFn>(fn, initialOptions): Throttler<TFn>
new Throttler<TFn>(fn, initialOptions): Throttler<TFn>

定义于: throttler.ts:145

参数

fn

TFn

initialOptions

ThrottlerOptions<TFn>

Returns (返回)

Throttler<TFn>

属性

fn

ts
fn: TFn;
fn: TFn;

定义于: throttler.ts:146


key

ts
key: undefined | string;
key: undefined | string;

定义于: throttler.ts:141


options

ts
options: ThrottlerOptions<TFn>;
options: ThrottlerOptions<TFn>;

定义于: throttler.ts:142


store

ts
readonly store: Store<Readonly<ThrottlerState<TFn>>>;
readonly store: Store<Readonly<ThrottlerState<TFn>>>;

定义于: throttler.ts:138

方法

cancel()

ts
cancel(): void
cancel(): void

定义于: throttler.ts:305

取消任何挂起的尾触发执行并清除内部状态。

如果安排了尾触发执行(由于设置了 trailing=true 进行节流),这将阻止该执行发生。内部的超时和存储的参数将被清除。

如果没有挂起的执行,则没有效果。

Returns (返回)

void


flush()

ts
flush(): void
flush(): void

定义于: throttler.ts:283

立即处理当前挂起的执行

Returns (返回)

void


maybeExecute()

ts
maybeExecute(...args): void
maybeExecute(...args): void

定义于: throttler.ts:224

尝试执行节流函数。执行行为取决于节流器选项

  • 如果自上次执行以来已经过去了足够的时间(>=等待周期)

    • 当 leading=true 时:立即执行
    • 当 leading=false 时:等待下一次尾部执行
  • 在等待周期内

    • 当 trailing=true 时:安排在等待周期结束时执行
    • 当 trailing=false 时:丢弃执行

参数

args

...Parameters<TFn>

Returns (返回)

void

示例

ts
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()

ts
reset(): void
reset(): void

定义于: throttler.ts:316

将节流器的状态重置为其默认值

Returns (返回)

void


setOptions()

ts
setOptions(newOptions): void
setOptions(newOptions): void

定义于: throttler.ts:166

更新节流器选项

参数

newOptions

Partial<ThrottlerOptions<TFn>>

Returns (返回)

void

我们的合作伙伴
Code Rabbit
Unkey
订阅 Bytes

您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。

Bytes

无垃圾邮件。您可以随时取消订阅。

订阅 Bytes

您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。

Bytes

无垃圾邮件。您可以随时取消订阅。