1.0.0 • Published 2 years ago
@helper-modules/throttle v1.0.0
@helper-modules/throttle
Allows to Throttle function calls.
Installation
npm i @helper-modules/throttle
Usage
Syntex
throttle(callback: Function, delay: Number);
- Params:
callback
:- type:
Function
- default:
undefined
- usage: Callback that will throttle
- type:
delay
:- type:
Number
- default:
undefined
- usage: Delay in milliseconds
- type:
Example
- Throttle is helpful when you want to call a function only once in some millisecond. It helps to avoid the function call when the function is called again within that delay.
const throttle = require("..");
const callback = (msg) => {
console.log(msg);
}
const throttledCallback = throttle(callback, 1000);
let n = 1;
let interval = setInterval(() => {
console.log(`interval: ${n}`);
throttledCallback(`callback called: ${n}`);
n++
}, 500);
setTimeout(() => {
clearInterval(interval);
console.log("interval cleared")
}, 2000);
1.0.0
2 years ago