https://github.com/fabiospampinato/isotimer
An isomorphic setImmediate/setInterval/setTimeout implementation.
https://github.com/fabiospampinato/isotimer
interval isomorphic ref timeout timer unref
Last synced: 10 months ago
JSON representation
An isomorphic setImmediate/setInterval/setTimeout implementation.
- Host: GitHub
- URL: https://github.com/fabiospampinato/isotimer
- Owner: fabiospampinato
- License: mit
- Created: 2025-01-26T23:23:59.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-01-26T23:25:29.000Z (over 1 year ago)
- Last Synced: 2025-04-23T22:17:22.734Z (about 1 year ago)
- Topics: interval, isomorphic, ref, timeout, timer, unref
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# IsoTimer
An isomorphic setImmediate/setInterval/setTimeout implementation.
This package exists to smooth out differences between Node and the browser, and to clean up some nonsense.
## Features
- This library works everywhere, with the same API.
- setTimeout/setInterval don't return numbers under Node, this library eliminates that ugliness.
- setImmediate is not natively available in the browser, this library polyfills it.
- setImmediate will also return a number, for consistency.
- .ref/.unref methods are not browser functions, this library exposes dedicated functions for them.
- If the number of milliseconds is too high it would [overflow](https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#maximum_delay_value), this library clamps it within the supported range.
## Install
```sh
npm install isotimer
```
## Usage
Very similar APIs are available for setImmediate/setInterval/setTimeout.
The following code will just look at timeouts, for convenience, but they all work the same way basically.
```ts
import {setImmediate, clearImmediate, refImmediate, unrefImmediate} from 'isotimer';
import {setInterval, clearInterval, refInterval, unrefInterval} from 'isotimer';
import {setTimeout, clearTimeout, refTimeout, unrefTimeout} from 'isotimer';
// Let's schedule a timeout, which will always return us a number
const timeoutId = setTimeout ( () => {
console.log ( 'Hello' );
}, 1000 );
// Let's unref and ref it back again, just to show how to do it
unrefTimeout ( timeoutId );
refTimeout ( timeoutId );
// Let's clear it
clearTimeout ( timeoutId );
```
## License
MIT © Fabio Spampinato