exact-ticker
v0.3.4
Published
Custom setinterval that won't start drifting
Downloads
12
Readme
Exact ticker
The exact ticker can be a replacement for setInterval.
When using setInterval the time will start to drift slowly, and the drift will keep increasing.
This class tries to solve this problem by using a startime and calculating the drift every interval, and adapt the new intervaltime. It is still possible to have a small drift, but it will be corrected after every interval.
It also adds the possibility to add a repeat count to the class. And if you add a repeat count you can also add a finishHandler.
Options
callBackFunction(required). The callback will be triggerd every time the interval is run.
interval(optional) The intervals (in milliseconds) on how often to execute the code. If no value is given the default intervalTime will be 1000ms
repeatCount(optional) The number of times the interval should run.
finishHandler(optional) will be triggered when the repeatCount is reached, if a finish handler is given, the callbackfunction won't be triggered if the repeatCount is reached
callBackFunction
Most basic exaple will keep running for ever every second/1000ms, the default interval time is 1000ms, so no need to pass the time.
const ExactTicker = require('exact-ticker');
const callBackFunction = val => console.log(val,"callBackFunction");
const exactTicker = new ExactTicker({
callBackFunction,
});
exactTicker.start();
Example with repeat count. The callbackfunction will be called 10 times
const ExactTicker = require('exact-ticker');
const callBackFunction = val => console.log(val,"callBackFunction");
const exactTicker = new ExactTicker({
repeatCount: 10,
callBackFunction,
});
exactTicker.start();
Example with repeat count and finishHandl. The callbackfunction will be called 9 times and 10th time it will call the finish handler
const ExactTicker = require('exact-ticker');
const callBackFunction = val => console.log(val,"callBackFunction");
const finishHandler = val => console.log("finishHandler");
const exactTicker = new ExactTicker({
repeatCount: 10,
callBackFunction,
finishHandler
});
exactTicker.start();