Debounce create a new function g
, which when called will delay the invocation of the original function f
until n
milliseconds after it was last called.
It's very useful for scenarios where it's better to limit the number of times the function is called. E.g. think of search input which fetches data from API. It's enough display search results after user stopped entering characters for some time.
import { debounce } from 'ts-debounce';
const debouncedFunction = debounce(originalFunction, waitMilliseconds, options);
originalFunction
- the function which we want to debounce
waitMilliseconds
- how many seconds must pass after most recent function call, for the original function to be called
options
- options object supports now one argument
isImmediate
- if set to
true
thenoriginalFunction
will be called immediately, but on subsequent calls of the debounced function original function won't be called, unlesswaitMilliseconds
passed after last call
- if set to
This implementation is based upon following sources:
- JavaScript Debounce Function by David Walsh
- Lodash implementation
- version 2 - TypeScript 3.3
- version 1 - TypeScript 2.0
Zac 💻 |
Karol Majewski 💻 |
Fabien Rogeret 💻 |
Iman 💻 |
- Project tries to adhere to all-contributors specification