定义于: debouncer.ts:129
创建一个防抖函数的类。
防抖确保函数仅在自上次调用后经过一段时间后才会被执行。这对于处理频繁事件(如窗口大小调整、滚动事件或输入更改)非常有用,因为您希望限制执行速率。
防抖函数可以配置为在延迟周期的开始(前沿)或结束(后沿,默认)执行。在等待期间的每个新调用都会重置计时器。
状态管理
const debouncer = new Debouncer((value: string) => {
saveToDatabase(value);
}, { wait: 500 });
// Will only save after 500ms of no new input
inputElement.addEventListener('input', () => {
debouncer.maybeExecute(inputElement.value);
});
const debouncer = new Debouncer((value: string) => {
saveToDatabase(value);
}, { wait: 500 });
// Will only save after 500ms of no new input
inputElement.addEventListener('input', () => {
debouncer.maybeExecute(inputElement.value);
});
• TFn extends AnyFunction
new Debouncer<TFn>(fn, initialOptions): Debouncer<TFn>
new Debouncer<TFn>(fn, initialOptions): Debouncer<TFn>
定义于: debouncer.ts:137
TFn
DebouncerOptions<TFn>
Debouncer<TFn>
fn: TFn;
fn: TFn;
定义于: debouncer.ts:138
key: string;
key: string;
定义于: debouncer.ts:133
options: DebouncerOptions<TFn>;
options: DebouncerOptions<TFn>;
定义于: debouncer.ts:134
readonly store: Store<Readonly<DebouncerState<TFn>>>;
readonly store: Store<Readonly<DebouncerState<TFn>>>;
定义于: debouncer.ts:130
cancel(): void
cancel(): void
定义于: debouncer.ts:268
取消任何待处理的执行
void
flush(): void
flush(): void
定义于: debouncer.ts:251
立即处理当前挂起的执行
void
maybeExecute(...args): void
maybeExecute(...args): void
定义于: debouncer.ts:204
尝试执行防抖函数。如果已有调用正在进行,它将被加入队列
...Parameters<TFn>
void
reset(): void
reset(): void
定义于: debouncer.ts:279
将去抖器状态重置为其默认值
void
setOptions(newOptions): void
setOptions(newOptions): void
定义于: debouncer.ts:158
更新防抖函数选项
Partial<DebouncerOptions<TFn>>
void
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。