d-bounce
Simple javascript debounce tool
Getting started
$ npm install d-bounce
const dBounce = require('d-bounce');
function sayWord(word) {
console.log(word);
}
const arguments = ['bounced'];
dBounce(sayWord, 'say-word-debounce', 3000, arguments);
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.
function exampleCallback() {
console.log('I am an example');
}
dBounce(exampleCallback, 'example-debounce', 3000, arguments);
dBounce(null, 'example-debounce');
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
.
dBounce(() => console.log('callback 1'), 'example-debounce-1');
dBounce(() => console.log('callback 2'), 'example-debounce-2');
The last callback function that dBounce is called with when the delay
is satisfied will be the function that gets invoked.
dBounce(() => console.log('callback 1'), 'example-debounce');
dBounce(() => console.log('callback 2'), 'example-debounce');
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.
function callback(one, two, three) {
console.log(one);
console.log(two);
console.log(three);
}
const arguments = ['first arg', 'second arg', 'third arg'];
dBounce(callback, 'example-debounce', 1000, arguments);
Testing
$ npm test