@util.js/node-timers
v0.41.3
Published
An object to hold JavaScript timers: https://nodejs.org/api/timers.html
Downloads
6
Maintainers
Readme
@util.js/node-timers
An object to hold JavaScript timers: https://nodejs.org/api/timers.html
@util.js/node-timers is part of Util.js.
Timers
timers.throttle(functionToThrottle, limitInMilliseconds) ⇒ Promise
Returns an asynchronous function that throttles the specified functionToThrottle calling it once every limitInMilliseconds.
Kind: instance method of Timers
Returns: Promise - A throttled version of functionToThrottle
Throws:
- TypeError If functionToThrottle is not a function
- TypeError If limitInMilliseconds is a Symbol
| Param | Type | Description | | ------------------- | --------------------- | -------------------------------------------------------------------------------------------------------------------------------- | | functionToThrottle | function | The function to wrap | | limitInMilliseconds | Number | The minimum amount of time between calls to functionToThrottle; if this value cannot be coerced into a number, then 0 is assumed |
Example
const timers = require("@util.js/node-timers");
let lastTime = Date.now();
function call(str) {
console.log(str + ": " + (Date.now() - lastTime));
lastTime = Date.now();
}
const throttledCall = timers.throttle(call, 2000);
throttledCall("a"); // should output "a: ~1".
throttledCall("b"); // should output "b: ~2000".
throttledCall("c"); // should output "c: ~2000".