funkz-debounce
v1.0.1
Published
A debounce function delays the execution of a given function until after a specified time has passed since the last time it was called — helping to limit how often the function runs, especially during rapid events like typing or scrolling.
Downloads
5
Maintainers
Readme
funkz-debounce
A lightweight and strongly typed debounce utility written in TypeScript.
It helps control how often a function is executed by delaying its execution until a specified amount of time has passed since the last call.
Installation
npm i funkz-debounceUsage
import debounce from "funkz-debounce";
function handleInput(value: string) {
console.log("Search:", value);
}
// Create a debounced version of the function
const debouncedSearch = debounce(handleInput, 500);
// Call multiple times, but it only triggers once after 500ms
debouncedSearch("apple");
debouncedSearch("banana");
debouncedSearch("orange");Function Signature
debounce<T extends (...args: Array<unknown>) => void>(
func: T,
delay: number,
immediate: boolean = false
): (...args: Parameters<T>) => void;Parameters:
- func : The function to debounce.
- delay : The time (in milliseconds) to wait before executing.
- immediate : If true, run the function immediately on the first call, then ignore subsequent calls until the delay passes.
Example with Immediate Execution
const logClick = debounce(() => console.log("Clicked!"), 300, true);
document.addEventListener("click", logClick);In this case, the function executes immediately on the first click, then ignores any other clicks for the next 300ms.
License
MIT © 2025 Ariel Francis Fernando Gacilo
