@ircam/ticker
Execute a given callback at periodic interval, trying to minimize drift and jitter.
Install
npm install [--save] @ircam/ticker
Usage
import Ticker from '@ircam/ticker'
const period = 50; // in milliseconds
const ticker = new Ticker(period, (logicalTime, currentTime, error) => {
console.log(logicalTime, currentTime, error);
});
ticker.start();
API
Ticker
Precise periodic timer (based on setTimeout
) that monitor and adapt itself
to stay close to the given therical period. In particular, try to minimize
the drift caused by the use of a raw setTimeout
.
Observed average jitter is around +/- 2ms.
Kind: global class
new Ticker(period, callback, options)
Param | Type | Description |
---|---|---|
period | Number |
period of the timer interval in milliseconds (floored if float is given) |
callback | TickerCallback |
callback to execute on each tick |
options | Object |
additionnal options |
[options.errorThreshold] | Number |
Threshold error where the timer considers itself as out of bounds. Increasing this value tends to increase the overall jitter. |
Number
ticker.period : Period of the timer. Must be an integer, the given value is floored. When updated the new value is applied at the next tick.
Kind: instance property of Ticker
ticker.start()
Start the ticker instance.
Kind: instance method of Ticker
ticker.stop()
Stop the ticker instance.
Kind: instance method of Ticker
function
TickerCallback : Kind: global typedef
Param | Type | Description |
---|---|---|
logicalTime | Number |
logical time since start in ms |
currentTime | Number |
current time as returned by performance.now
|
error | Number |
current error |
License
BSD-3-Clause