throttle-debounce-ts
v1.1.1
Published
Throttle and debounce functions written in typescript
Downloads
1,321
Readme
throttle-debounce-ts ·
Throttle and debounce functions written in Typescript.
Features:
- No thirdparty dependecies
- Easy to use
- ES Modules and CommonJS format
Install
npm i throttle-debounce-ts
Usage
Throttle
import { throttle } from "throttle-debounce-ts";
const throttleFunc = throttle(1000, () => {
console.log("Hello Throttle!");
});
Debounce
import { debounce } from "throttle-debounce-ts";
const debounceFunc = debounce(1000, () => {
console.log("Hello Debounce!");
});
Cancel
const throttleFunc = throttle(1000, () => {
// ...
});
// will cancel throttleFunc
throttleFunc.cancel();
const debounceFunc = debounce(1000, () => {
// ...
});
// will cancel delay of debounceFunc
// callback will exec immediately when you call debounceFunc next time
debounceFunc.cancel();
API
throttle(options, callback)
Returns: Function
Throttle execution of a function.
options
Type: Number
A zero-or-greater delay in milliseconds.
Type: Object
options.delay A zero-or-greater delay in milliseconds.
options.leading Optional, defaults to false. If it's true, the function will exec on the first call.
options.trailing Optional, defaults to false. If it's true, the function will exec after last call.
callback
Type: Function
A function to be executed after delay milliseconds.
debounce(options, callback)
Returns: Function
Debounce execution of a function.
options
Type: Number
A zero-or-greater delay in milliseconds.
Type: Object
options.delay A zero-or-greater delay in milliseconds.
options.leading Optional, defaults to false. If it's true, the function will exec on the first call.
callback
Type: Function
A function to be executed after delay milliseconds.