https://github.com/televisionninja/no-drift
reduced drift timeouts and intervals
https://github.com/televisionninja/no-drift
drift interval timeout
Last synced: 9 months ago
JSON representation
reduced drift timeouts and intervals
- Host: GitHub
- URL: https://github.com/televisionninja/no-drift
- Owner: TelevisionNinja
- License: mit
- Created: 2021-03-15T00:08:33.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2022-03-24T06:10:24.000Z (over 4 years ago)
- Last Synced: 2025-08-29T11:58:34.108Z (10 months ago)
- Topics: drift, interval, timeout
- Language: JavaScript
- Homepage:
- Size: 42 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# no-drift
Reduced drift timeouts and intervals for node.js
Inspired by [driftless](https://github.com/dbkaplun/driftless)
## Usage
```javascript
// import any implementation you want
import {
// default implementation
setNoDriftInterval,
setNoDriftTimeout,
clearNoDrift,
// setImmediate implementation
setNoDriftZeroInterval,
setNoDriftZeroTimeout,
clearNoDriftZero,
// worker thread implementation
setNoDriftWorkerInterval,
setNoDriftWorkerTimeout,
clearNoDriftWorker
} from 'no-drift';
//------------------
// similar usage to setTimeout and setInterval
// all implementations have the same usage
setNoDriftTimeout(() => {
console.log('Hello world 1');
});
setNoDriftTimeout(() => {
console.log('Hello world 2');
}, 1000);
setNoDriftTimeout((a, b, c) => {
console.log(a, b, c);
}, 2000, '1', '2', '3');
setNoDriftTimeout("console.log('Hello world 3');", 3000);
// nodrift intervals have the same usage shown above
//------------------
// clearing nodrift
const id = setNoDriftTimeout(() => {
console.log('clear');
}, 1000);
clearNoDrift(id);
// each implementation has their own pool of IDs
// so import and use the appropriate clearing function
```
## Formulas for timeout times:
_r_ = rate
_t_ = total time
### Time left
__recurrence relation__
_d_0 = _t_
_d__n_ = (1 - _r_)_d__n_-1
__closed form__
_d__n_ = _t_(1 - _r_)_n_
### Current time
__recurrence relation__
_c_0 = 0
_c__n_ = (1 - _r_)_c__n_-1 + _tr_
__closed form__
_c__n_ = _t_(1 - (1 - _r_)_n_)
### Current wait time
__recurrence relation__
_w_0 = _tr_
_w__n_ = (1 - _r_)_w__n_-1
__closed form__
_w__n_ = _tr_(1 - _r_)_n_