A simple debounce utility function with cancel, cancelAndExecute, and pending support.
npm install @marcopollacci/debouncingimport { debouncing } from "@marcopollacci/debouncing";
// Basic usage
const log = debouncing(() => console.log("Hello"));
log(); // waits 500ms before printing 'Hello' to the console
// Custom wait time
const log2 = debouncing(() => console.log("Hello after 1s"), 1000);
log2(); // waits 1000ms before printing- Support for cancel, cancelAndExecute, and pending methods
const log = debouncing(() => console.log("Hello"), 1000);
log(); // schedules execution in 1s
// Cancel the pending execution
log.cancel();
// Check if execution is pending
console.log(log.pending()); // false (because we canceled)const log = debouncing(() => console.log("Hello"), 1000);
log(); // schedules execution in 1s
// Cancel the pending execution and execute immediately
console.log(log.cancelAndExecute()); // HelloThis project uses Bun as the test runner.
npx bun test index.test.jsMIT