![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
An accurate timer utility for running periodic tasks on the given interval ticks or dates. (Node and Browser)
© 2019, Onur Yıldırım (@onury).
An accurate timer utility for running periodic tasks on the given interval ticks or dates.
TaskTimer
?Because of the single-threaded, asynchronous nature of JavaScript, each execution takes a piece of CPU time, and the time they have to wait will vary, depending on the load. This creates a latency and cumulative difference in asynchronous timers; that gradually increase the inacuraccy. 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
.npm i tasktimer
In Node/CommonJS environments:
const { TaskTimer } = require('tasktimer');
With transpilers (TypeScript, Babel):
import { TaskTimer } from 'tasktimer';
In (Modern) Browsers:
<script src="js/tasktimer.min.js"></script>
<script>
const { TaskTimer } = tasktimer;
</script>
const timer = new TaskTimer(1000);
timer.add(task => console.log(`Current runs: ${task.currentRuns}`)).start();
const timer = new TaskTimer(5000);
timer.on('tick', () => console.log(`Tick count: ${timer.tickCount}`));
timer.start();
// Timer with 1000ms (1 second) base interval resolution.
const timer = new TaskTimer(1000);
// interval can be updated anytime by setting the `timer.interval` 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.`);
}
},
{
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(`${task.id} task has run ${task.currentRuns} times.`);
}
}
]);
// 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();
1000
milliseconds), to be used as base resolution (tick) for the tasks.tick
(base interval) or task
run, etc...See API reference and examples here.
See CHANGELOG.md.
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
Install dependencies:
npm install
Add tests into test/node and test/browser and run:
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.
MIT.
3.0.0 (2019-08-02)
timer#time.elapsed
was a timestamp when idle but a timespan when running. Fixes #11.FAQs
An accurate timer utility for running periodic tasks on the given interval ticks or dates. (Node and Browser)
The npm package tasktimer receives a total of 5,671 weekly downloads. As such, tasktimer popularity was classified as popular.
We found that tasktimer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.