tasktimer2.0.1 • Public • Published
© 2019, Onur Yıldırım (@onury).
An accurate timer utility for running periodic tasks on the given interval ticks or dates.
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.
- Precission & Accuracy: With the
precisionoption (enabled by default);
- The delay between each tick is auto-adjusted when it's off due to task/CPU loads or clock drifts.
- In Node.js,
TaskTimeralso 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.
- The timer may hit a synchronous / blocking task; or detect significant time drift (longer than the base interval) due to JS event queue, which cannot be recovered by simply adjusting the next delay. In this case, right from the next tick onward; it will auto-recover as much as possible by running "immediate" tasks until it reaches the proper time vs tick/run balance.
- Run or schedule multiple tasks (on a single timer instance).
- Ability to run sync or async tasks that return a promise (or use callbacks).
- Ability to balance task-loads via distributing executions by tick intervals.
- Ability to limit total runs of a task.
- Stateful tasks: i.e. ability to auto-stop when all tasks complete.
TaskTimeris also an
- Universal module. Works in both Node and Browser.
- Small size (less than 6kB minified, gzipped).
- Completely re-written in TypeScript. (version 2.0.0+)
npm i tasktimer
In Node/CommonJS environments:
const TaskTimer = ;
With transpilers (TypeScript, Babel):
In (Modern) Browsers:
const timer = 1000;timerstart;
Regular Timer (without Task Management)
const timer = 5000;timer;timerstart;
// Timer with 1000ms (1 second) base interval resolution.const timer = 1000;// interval can be updated anytime by setting the `timer.interval` property.// Add multiple tasks (at once) based on tick intervals.timer;// You can also execute some code on each tick... (every 1000 ms)timer;// Start the timertimerstart;
How it works
- When you create a timer; you set a time-interval (e.g.
1000milliseconds), to be used as base resolution (tick) for the tasks.
- 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
tick(base interval) or
- 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.
If you're migrating from TaskTimer v1 to v2+, there are various breaking changes!..
Clone original project:
git clone https://github.com/onury/tasktimer.git
npm run test! # builds and runs testsnpm test # runs tests without building
editorconfig for style and linting.
Travis build should pass, coverage should not degrade.