fn-debounce
v1.0.4
Published
A simple debounce function for JavaScript
Downloads
2
Readme
Debounce Function
A lightweight module module to help you execute a function after a certain wait time, since the last time it was called. Avoids executing the function too many times when the user interacts continuously (e.g., scrolling, typing)
Usage
import debounce from 'fn-debounce';
const myFunction = () => {
// ...
};
const debouncedFunction = debounce(myFunction, 1000);
// Call debouncedFunction multiple times
debouncedFunction();
debouncedFunction();
debouncedFunction();
// After 1000ms, myFunction will only be executed once
Parameters
func
: The function to debouncewait
: The wait time in millisecondsimmediate
: (Optional) Iftrue
, the function will be executed immediately, then wait wait ms before executing the next time. Default isfalse
.
Usage examples
- Debounce the click event of a button to avoid submitting a form repeatedly
- Debounce the scroll event to only update content when the user stops scrolling
- Debounce the keyup event to only perform a search after the user has finished typing a string
Contributing
You can contribute to this package by submitting a pull request on GitHub.