no-drift

2.0.2 • Public • Published

no-drift

Reduced drift timeouts and intervals for node.js

Inspired by driftless

Usage

// 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

d0 = t

dn = (1 - r)dn-1

closed form

dn = t(1 - r)n

Current time

recurrence relation

c0 = 0

cn = (1 - r)cn-1 + tr

closed form

cn = t(1 - (1 - r)n)

Current wait time

recurrence relation

w0 = tr

wn = (1 - r)wn-1

closed form

wn = tr(1 - r)n

Package Sidebar

Install

npm i no-drift

Weekly Downloads

5

Version

2.0.2

License

MIT

Unpacked Size

15.3 kB

Total Files

8

Last publish

Collaborators

  • televisionninja