https://github.com/fabiospampinato/dettle
A tiny fully-featured debounce and throttle implementation.
https://github.com/fabiospampinato/dettle
debounce throttle tiny
Last synced: 8 months ago
JSON representation
A tiny fully-featured debounce and throttle implementation.
- Host: GitHub
- URL: https://github.com/fabiospampinato/dettle
- Owner: fabiospampinato
- License: mit
- Created: 2023-01-27T19:51:29.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-01-26T23:38:10.000Z (over 1 year ago)
- Last Synced: 2025-03-15T12:05:56.526Z (about 1 year ago)
- Topics: debounce, throttle, tiny
- Language: TypeScript
- Homepage:
- Size: 15.6 KB
- Stars: 20
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# Dettle
A tiny fully-featured debounce and throttle implementation.
## Install
```sh
npm install dettle
```
## Usage
```ts
import {debounce, throttle} from 'dettle';
const fn = () => console.log ( 'Fired!' );
// Debouncing
// The following options are supported:
// `leading`: whether the function should be called when the timeout is created, defaults to `false`
// `trailing`: whether the function should be called when the timeout expires, defaults to `true`
// `maxWait`: the maximum amount of time that can pass before the function is called, defaults to `Infinity`
const debounced = debounce ( fn, 1000, {
leading: false,
trailing: true,
maxWait: Infinity
});
debounced (); // Schedule function for execution
debounced (); // Re-schedule function for execution
debounced.flush (); // Execute the function immediately, if there's a scheduled execution
debounced.cancel (); // Cancel the scheduled execution
// Throttling
// The API for throttling is basically the same, except that:
// - `leading`: is `true` by default rather than `false`
// - `maxWait`: is set implicitly for you to be equal to the wait time
const throttled = throttle ( fn, 1000, {
leading: true,
trailing: true
});
throttled (); // Call the function immediately
throttled (); // Schedule function for execution
throttled.flush (); // Execute the function immediately, if there's a scheduled execution
throttled.cancel (); // Cancel the scheduled execution
```
## License
MIT © Fabio Spampinato