d-bounce
Simple javascript debounce tool
Getting started
$ npm install d-bounce
// import d-bounceconst dBounce = ; // callback function to be invoked after delay { console;} // list of arguments to be passed to the callback function on invocationconst arguments = 'bounced'; // this will console.log the word 'bounced' after 3 seconds; /* * if dBounce is called again within 3 seconds * with the same identifier string 'say-word-debounce' * the clock will reset and another 3 seconds will be * waited before the callback is invoked. */
API
d-bounce accepts 4 arguments although only one is necessary.
dBounce(callback
[, identifier
[, delay
[, arguments
]]])
callback
(Function
or null
or undefined
) required
The function that you wish to be invoked after the delay provided dBounce is not called again with the same identifier
.
Passing either null
or undefined
will cancel the debounce for the given identifier.
{ console;}; ;// debounce will be cancelled before it invokes the callback
identifier
(String
) optional default: 'default'
A string identifier used to associate each attempted invocation with other attempted invocations. calls to d-bounce are linked to each other by the identifier
. This means that you could pass a different callback function every time you call dBounce, using the same identifier
, and dBounce will reset the clock on the delay
.
// create a debpunced function with the identifier 'example-debounce-1'; // create a second debpunced function with the identifier 'example-debounce-2'; // both callbacks will be invoked as they have different identifiers.// one has no effect on the other
The last callback function that dBounce is called with when the delay
is satisfied will be the function that gets invoked.
;;// the the second callback that logs out 'callback 2' will be the only one that is invoked.
delay
(Number
) optional - default: 300
default: 300
The amount of time waited by dBounce before invoking the callback, provided debounce is not called again with the same identifier
.
arguments
(Array
) optional - default: []
An array of arguments to be passed to the callback upon invocation.
{ console; console; console;} const arguments = 'first arg' 'second arg' 'third arg'; ; /* * After 1 second the callback will be invoked and * the console will log out the following * * first arg * second arg * third arg */
Testing
$ npm test