An accurate timer utility for running periodic tasks on the given interval ticks or dates. (Node and Browser)
npm install tasktimer
© 2019, Onur Yıldırım (@onury).
An accurate timer utility for running periodic tasks on the given interval ticks or dates.
TaskTimer?TaskTimer claims to be the best timer that overcomes this problem as much as possible.Secondly, I needed a task manager that can handle multiple tasks on different intervals, with a single timer instance.
precision option (enabled by default);TaskTimer also makes use of process.hrtime() high-resolution real-time. The time is relative to an arbitrary time in the past (not related to the time of day) and therefore not subject to clock drifts.TaskTimer is also an EventEmitter.``sh`
npm i tasktimer
In Node/CommonJS environments:
`js`
const { TaskTimer } = require('tasktimer');
With transpilers (TypeScript, Babel):
`js`
import { TaskTimer } from 'tasktimer';
In (Modern) Browsers:
`html`
`jsCurrent runs: ${task.currentRuns}
const timer = new TaskTimer(1000);
timer.add(task => console.log()).start();`
`jsTick count: ${timer.tickCount}
const timer = new TaskTimer(5000);
timer.on('tick', () => console.log());`
timer.start();
`jstimer.interval
// Timer with 1000ms (1 second) base interval resolution.
const timer = new TaskTimer(1000);
// interval can be updated anytime by setting the property.
// Add multiple tasks (at once) based on tick intervals.
timer.add([
{
id: 'task-1', // unique ID of the task
tickInterval: 5, // run every 5 ticks (5 x interval = 5000 ms)
totalRuns: 10, // run 10 times only. (set to 0 for unlimited times)
callback(task) {
// code to be executed on each run
console.log(${task.id} task has run ${task.currentRuns} times.);${task.id} task has run ${task.currentRuns} times.
}
},
{
id: 'task-2', // unique ID of the task
tickDelay: 1, // 1 tick delay before first run
tickInterval: 10, // run every 10 ticks (10 x interval = 10000 ms)
totalRuns: 2, // run 2 times only. (set to 0 for unlimited times)
callback(task) {
// code to be executed on each run
console.log();
}
}
]);
// You can also execute some code on each tick... (every 1000 ms)
timer.on('tick', () => {
console.log('tick count: ' + timer.tickCount);
console.log('elapsed time: ' + timer.time.elapsed + ' ms.');
// stop timer (and all tasks) after 1 hour
if (timer.tickCount >= 3600000) timer.stop();
});
// Start the timer
timer.start();
`
- When you create a timer; you set a time-interval (e.g. 1000 milliseconds), to be used as base resolution (tick) for the tasks.tick
- Then add task(s) to be executed on tick-intervals.
(e.g. task1 runs on every 10th tick, task2 runs on every 30th)
- You can optionally define:
- The number of total runs,
- An initial delay,
- Start/end dates for each task...
- In addition to task callbacks; event listeners can be added to execute some other code on each (base interval) or task run, etc...
- You can add, remove, reset, disable individual tasks at any time, without having to stop or re-create the timer.
- Pause and resume the timer at any time; which effects all current tasks.
See [API reference][docs] and examples [here][docs].
See [CHANGELOG.md][changelog].
If you're migrating from TaskTimer v1 to v2+, there are various breaking changes!..
Clone original project:
`sh`
git clone https://github.com/onury/tasktimer.git
Install dependencies:
`sh`
npm install
Add tests into test/node and test/browser and run:
`sh`
npm run test! # builds and runs tests
npm test # runs tests without building
Use included tslint.json and editorconfig` for style and linting.
Travis build should pass, coverage should not degrade.
[docs]:https://onury.io/tasktimer/api
[changelog]:CHANGELOG.md
[how-timers-work]:https://johnresig.com/blog/how-javascript-timers-work/
[clock-drift]:https://en.wikipedia.org/wiki/Clock_drift