debounce-callback
v1.0.0
Published
debounceCallback works similar to useDebounceCallback hook in @react-hook/debounce. However debounceCallback is not a hook in react.
Downloads
5
Maintainers
Readme
Table of contents
Introduction
debounceCallback
works similar touseDebounceCallback
hook in@react-hook/debounce
library. HoweverdebounceCallback
is not a hook in react.debounceCallback
can work in the backend-node and in other frameworks like ReactJs, VueJs, React Native,....- Module :
CommonJs
How to use ?
Example 1 - Typescript/NodeEnvironment
import { sleep } from 'sakura-time-service';
import { debounceCallback } from 'debounce-callback';
async function main(){
const sums:number[] = [];
const calcSumDebounce = debounceCallback(async (a: number, b: number) => {
sums.push(a + b);
}, 600); // wait 600 ms
for (let i = 0; i < 10; i++) {
calcSumDebounce(5, i);
await sleep(100); // waiting for 100 ms
}
// latest invoke
calcSumDebounce(5, 10);
await sleep(1000); // waiting for 1000 ms
console.log('sums =', sums); // sums = [ 15 ]
}
main();
Example 2 - Javascript/NodeEnvironment
const { sleep } = require('sakura-time-service');
const { debounceCallback } = require('debounce-callback');
async function main(){
const sums = [];
const calcSumDebounce = debounceCallback(async (a, b) => {
sums.push(a + b);
}, 600); // wait 600 ms
for (let i = 0; i < 10; i++) {
calcSumDebounce(5, i);
await sleep(100); // waiting for 100 ms
}
// latest invoke
calcSumDebounce(5, 10);
await sleep(1000); // waiting for 1000 ms
console.log('sums =', sums); // sums = [ 15 ]
}
main();