A timer utility for running/scheduling periodic tasks on the given interval ticks. (Node and Browser)
Switch branches/tags
Nothing to show
Clone or download
Latest commit 44e2405 Aug 12, 2018

README.md

TaskTimer

npm release dependencies license maintained

© 2018, Onur Yıldırım (@onury). MIT License.

A timer utility for running periodic tasks on the given interval ticks.
This is useful when you want to run or schedule multiple tasks (on a single timer instance).

Universal module. Works in both Node and Browser. Less than 3.3 KB (minified, gzipped).

In order to build on more solid foundations, this library (v2.0.0+) is completely re-written in TypeScript.

How it works

  • When you create a timer; you set a time-interval, to be used as base resolution (tick) for the tasks.
  • Then add task(s) to be executed on tick-intervals.
  • You can optionally define number of total runs, an initial delay or start/end dates for each task.
  • Event listeners can be added to be invoked on each tick, task, etc...
  • Add, remove or reset individual tasks at anytime.
  • Start, pause, resume or stop the timer; which effects all current tasks.

Installation

npm i tasktimer --save

Usage

In Node/CommonJS environments:

const { TaskTimer } = require('tasktimer');

With transpilers (TypeScript, Babel):

import { TaskTimer } from 'tasktimer';

In Browsers:

<script src="js/tasktimer.min.js"></script>
<script>
    var TaskTimer = tasktimer.TaskTimer;
    // or in modern browsers:
    // const { TaskTimer } = tasktimer;
</script>

Simplest Example

const timer = new TaskTimer(1000);
timer.add(task => console.log(`Current runs: ${task.currentRuns}`)).start();

Regular Timer (without Task Management)

const timer = new TaskTimer(5000);
timer.on('tick', () => console.log(`Tick count: ${timer.tickCount}`));
timer.start();

Detailed Example

// 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([
    {
        name: 'task-1',     // unique name 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.name} task has run ${task.currentRuns} times.`);
        }
    },
    {
        name: 'task-2',     // unique name of the task
        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.name} task has run ${task.currentRuns} times.`);
        }
    }
]);

// You can also execute some code on each tick... (every 1 second)
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();

Documentation

See API documentation and examples here.

Changelog

See CHANGELOG.md.
If you're migrating from TaskTimer v1 to v2+, there are various breaking changes!..

Contributing

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.

License

MIT.