function debounce<TFn>(fn, initialOptions): (...args) => void
function debounce<TFn>(fn, initialOptions): (...args) => void
定义于: debouncer.ts:312
创建一个防抖函数,该函数会将提供的函数延迟调用,直到指定的等待时间结束。在等待期间的多次调用将取消之前待定的调用并重置计时器。
这是从 Debouncer 类中提取的简单函数包装器实现。如果您需要更多对防抖行为的控制,请直接使用 Debouncer 类。
如果 leading 选项为 true,函数将在第一次调用时立即执行,然后等待延迟,之后才允许再次执行。
状态管理
• TFn extends AnyFunction
TFn
DebouncerOptions<TFn>
Function
...Parameters<TFn>
void
const debounced = debounce(() => {
saveChanges();
}, { wait: 1000 });
// Called repeatedly but executes at most once per second
inputElement.addEventListener('input', debounced);
const debounced = debounce(() => {
saveChanges();
}, { wait: 1000 });
// Called repeatedly but executes at most once per second
inputElement.addEventListener('input', debounced);
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。