An open API service indexing awesome lists of open source software.

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

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_