Security News
New Proposed CISA Mandate Would Require Critical Infrastructure to Report Ransom Payments Within 24 Hours
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
tasktimer
Advanced tools
An accurate timer utility for running periodic tasks on the given interval ticks or dates. (Node and Browser)
Weekly downloads
Readme
© 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.
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 1,535 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
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
Security News
Redis is no longer OSS, breaking its explicit commitment to remain under the BSD 3-Clause License forever. This has angered contributors who are now working to fork the software.
Product
Socket AI now enables 'AI detected potential malware' alerts by default, ensuring users benefit from AI-powered state-of-the-art malware detection without needing to opt-in.