| 1 |
[![Build status][build-image]][build-url] |
| 2 |
[![Tests coverage][cov-image]][cov-url] |
| 3 |
[![npm version][npm-image]][npm-url] |
| 4 |
|
| 5 |
# timers-ext |
| 6 |
|
| 7 |
## Timers extensions |
| 8 |
|
| 9 |
### Installation |
| 10 |
|
| 11 |
$ npm install timers-ext |
| 12 |
|
| 13 |
To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: [Browserify](http://browserify.org/), [Webmake](https://github.com/medikoo/modules-webmake) or [Webpack](http://webpack.github.io/) |
| 14 |
|
| 15 |
### API |
| 16 |
|
| 17 |
#### MAX*TIMEOUT *(timers-ext/max-timeout)\_ |
| 18 |
|
| 19 |
Maximum possible timeout value in milliseconds. It equals to maximum positive value for 32bit signed integer, so _2³¹ (2147483647)_, which makes it around 24.9 days |
| 20 |
|
| 21 |
#### delay(fn[, timeout]) _(timers-ext/delay)_ |
| 22 |
|
| 23 |
Returns function which when invoked will call _fn_ function after specified |
| 24 |
_timeout_. If _timeout_ is not provided [nextTick](https://github.com/medikoo/next-tick/#next-tick) propagation is used. |
| 25 |
|
| 26 |
#### once(fn[, timeout]) _(timers-ext/once)_ |
| 27 |
|
| 28 |
Makes sure to execute _fn_ function only once after a defined interval of time (debounce). If _timeout_ is not provided [nextTick](https://github.com/medikoo/next-tick/#next-tick) propagation is used. |
| 29 |
|
| 30 |
```javascript |
| 31 |
var nextTick = require("next-tick"); |
| 32 |
var logFoo = function () { console.log("foo"); }; |
| 33 |
var logFooOnce = require("timers-ext/once")(logFoo); |
| 34 |
|
| 35 |
logFooOnce(); |
| 36 |
logFooOnce(); // ignored, logFoo will be logged only once |
| 37 |
logFooOnce(); // ignored |
| 38 |
|
| 39 |
nextTick(function () { |
| 40 |
logFooOnce(); // Invokes another log (as tick passed) |
| 41 |
logFooOnce(); // ignored |
| 42 |
logFooOnce(); // ignored |
| 43 |
}); |
| 44 |
``` |
| 45 |
|
| 46 |
#### validTimeout(timeout) _(timers-ext/valid-timeout)_ |
| 47 |
|
| 48 |
Validates timeout value. |
| 49 |
For `NaN` resolved _timeout_ `0` is returned. |
| 50 |
If _timeout_ resolves to a number: |
| 51 |
|
| 52 |
- for _timeout < 0_ `0` is returned |
| 53 |
- for _0 >= timeout <= [MAX_TIMEOUT](#max_timeout-timers-extmax-timeout)_, `timeout` value is returned |
| 54 |
- for _timeout > [MAX_TIMEOUT](#max_timeout-timers-extmax-timeout)_ exception is thrown |
| 55 |
|
| 56 |
### Tests |
| 57 |
|
| 58 |
$ npm test |
| 59 |
|
| 60 |
## Security contact information |
| 61 |
|
| 62 |
To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. |
| 63 |
|
| 64 |
[build-image]: https://github.com/medikoo/timers-ext/workflows/Integrate/badge.svg |
| 65 |
[build-url]: https://github.com/medikoo/timers-ext/actions?query=workflow%3AIntegrate |
| 66 |
[cov-image]: https://img.shields.io/codecov/c/github/medikoo/timers-ext.svg |
| 67 |
[cov-url]: https://codecov.io/gh/medikoo/timers-ext |
| 68 |
[npm-image]: https://img.shields.io/npm/v/timers-ext.svg |
| 69 |
[npm-url]: https://www.npmjs.com/package/timers-ext |