Debounce
Delay function calls until a set time elapses after the last invocation
Install / Use
/learn @sindresorhus/DebounceREADME
debounce
Delay function calls until a set time elapses after the last invocation
Install
npm install debounce
Usage
import debounce from 'debounce';
function resize() {
console.log('height', window.innerHeight);
console.log('width', window.innerWidth);
}
window.onresize = debounce(resize, 200);
To check if the debounce delay is currently active:
window.onresize.isPending;
To later clear the timer and cancel currently scheduled executions:
window.onresize.clear();
Execute immediately only if a call is pending (and reset the timer):
window.onresize.flush();
To execute immediately and reset the timer if it was previously set:
window.onresize.trigger();
API
debounce(fn, wait, options?)
Creates a debounced function that delays execution until wait milliseconds have passed since its last invocation.
Set the immediate option to true to execute the function immediately at the start of the wait interval, preventing issues such as double-clicks on a button.
The returned function has the following methods:
.isPendingindicates whether the debounce delay is currently active..clear()cancels any scheduled executions..flush()if an execution is scheduled then it will be immediately executed and the timer will be cleared..trigger()executes the function immediately and clears the timer if it was previously set.
Related
- debounce-fn - Similar but with more options like
maxWaitandbefore/after - p-debounce - Similar but handles promises
- throttleit - Throttle a function to limit its execution rate
Related Skills
node-connect
347.9kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.9kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.9kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
