Custom and more accurate setInterval and setTimeout functionality that is not nerfed for things like when the browser tab is not in focus
- Roughly 99% accurate real time intervals in JavaScript
- More precision than a whole millisecond; fractional values supported
- Alternate version with only whole millisecond precision but with a smaller footprint on the JavaScript Event Loop
Install via:
npm install @y0urstruly/timer
- Main Timer Library
const {timeout,interval,wait,clear} = require('@y0urstruly/timer');
- Alternate Timer Library
const {timeout,interval,wait,clear} = require('@y0urstruly/timer/docile');
- Main Timer Library
import {timeout,interval,wait,clear} = from '@y0urstruly/timer';
- Alternate Timer Library
import {timeout,interval,wait,clear} = from '@y0urstruly/timer/docile';
interval(userFN:()=>void, ms:number): number
: better setInterval, first argument is function, second argument is ms, returns IDtimeout(userFN:()=>void, ms:number): number
: better setTimeout, first argument is function, second argument is ms, returns IDwait(ms:number): Promise<void>
: all you python sleep using kids, your functionality offered by a promise wrapped around timeout (tldr; await wait(ms))clear(ID:number): boolean
: removes any interval or timeout by ID. For example clear(timeout(someFN)) is a waste of code since someFN will never run
- It is more precise in nodejs (>300 events per ms
at least on a github codespace) - It is less precise in a browser (~10 events per ms
at least on a chrome tab) - The 2 points above were determined by comparing a 0ms interval to a 1ms interval and the ratio of their calls
- Of course, sub ms timers might lose precision since functions take time to execute (tested on codespace for 0.2ms and got 98% precision instead) so it is recommended to have fractional timer values greater than 1
- The smaller the fractional part, the less accurate (
1.1 would be more accurate than 2.01
)