Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/debounce
Delay function calls until a set time elapses after the last invocation
https://github.com/sindresorhus/debounce
Last synced: about 1 month ago
JSON representation
Delay function calls until a set time elapses after the last invocation
- Host: GitHub
- URL: https://github.com/sindresorhus/debounce
- Owner: sindresorhus
- License: other
- Created: 2013-08-21T18:23:25.000Z (about 11 years ago)
- Default Branch: main
- Last Pushed: 2023-12-21T20:40:22.000Z (11 months ago)
- Last Synced: 2024-05-22T18:23:13.243Z (6 months ago)
- Language: JavaScript
- Homepage:
- Size: 34.2 KB
- Stars: 713
- Watchers: 11
- Forks: 75
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
- Security: .github/security.md
Awesome Lists containing this project
README
# debounce
> Delay function calls until a set time elapses after the last invocation
## Install
```sh
npm install debounce
```## Usage
```js
import debounce from 'debounce';function resize() {
console.log('height', window.innerHeight);
console.log('width', window.innerWidth);
}window.onresize = debounce(resize, 200);
```*(You can also use `const debounce = require('debounce')`)*
To later clear the timer and cancel currently scheduled executions:
```js
window.onresize.clear();
```To execute immediately only if you have scheduled invocations and reset the timer:
```js
window.onresize.flush();
```To execute immediately and reset the timer if it was previously set:
```js
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:
- `.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
- [p-debounce](https://github.com/sindresorhus/p-debounce) - Similar but handles promises
- [throttleit](https://github.com/sindresorhus/throttleit) - Throttle a function to limit its execution rate