|
| 1 | +# timeout-controller |
| 2 | + |
| 3 | +[](https://travis-ci.org/jacobheun/timeout-controller) [](https://david-dm.org/jacobheun/timeout-controller) [](https://standardjs.com) |
| 4 | + |
| 5 | +> An AbortController that aborts after a specified timeout. |
| 6 | +
|
| 7 | +`timeout-controller` uses [`retimer`](https://github.com/mcollina/retimer) internally to help reduce the impact of having numerous timers running. |
| 8 | + |
| 9 | +## Install |
| 10 | + |
| 11 | +``` |
| 12 | +npm install timeout-controller |
| 13 | +``` |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +```js |
| 18 | +const AbortController = require('abort-controller') |
| 19 | +const TimeoutController = require('timeout-controller') |
| 20 | +const anySignal = require('any-signal') |
| 21 | + |
| 22 | +const userController = new AbortController() |
| 23 | +// Aborts after 1 second |
| 24 | +const timeoutController = new TimeoutController(1000) |
| 25 | + |
| 26 | +const combinedSignal = anySignal([userController.signal, timeoutController.signal]) |
| 27 | +combinedSignal.addEventListener('abort', () => console.log('Abort!')) |
| 28 | + |
| 29 | +// The user or the timeout can now abort the action |
| 30 | +await performSomeAction({ signal: combinedSignal }) |
| 31 | +timeoutController.clear() |
| 32 | +``` |
| 33 | + |
| 34 | +## API |
| 35 | + |
| 36 | +### `new TimeoutController(ms)` |
| 37 | + |
| 38 | +Creates an [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) compliant `TimeoutController`. |
| 39 | + |
| 40 | +#### Parameters |
| 41 | + |
| 42 | +| Name | Type | Description | |
| 43 | +|------|------|-------------| |
| 44 | +| ms | number | The time in milliseconds for when the TimeoutController should abort | |
| 45 | + |
| 46 | +### `timeoutController.clear()` |
| 47 | + |
| 48 | +Clears the internal timer. |
| 49 | + |
| 50 | +### `timeoutController.abort()` |
| 51 | + |
| 52 | +Aborts the controller and clears the internal timeout. |
| 53 | + |
| 54 | +## Related |
| 55 | + |
| 56 | +[`abort-controller`](https://github.com/mysticatea/abort-controller) |
| 57 | +[`any-signal`](https://github.com/jacobheun/any-signal) Combines an array of AbortSignals into a single signal. |
| 58 | + |
| 59 | +## LICENSE |
| 60 | + |
| 61 | +[MIT](LICENSE) © Jacob Heun |
0 commit comments