real-scheduler

0.0.8 • Public • Published

real-scheduler

Build Status Coverage Status

Executes your callback repeatedly. Unlike the built-in setInterval method, real-scheduler avoids the excessive accumulation of timing errors in the long-run by making delay adjustments after each callback call.

Installation

npm install real-scheduler

Usage

executes a callback every delay milliseconds:

    new RealScheduler(callback, delay);

complete example

var RealScheduler = require('real-scheduler');

// create a Scheduler and run it automatically
// sch is a Scheduler's instance passed to a callback function
var scheduler = new RealScheduler((sch) => {
        console.log(
            // a real, accumulated time since the scheduler's start
            sch.getTimeElapsed() +
            // how many times this callback was called since the scheduler's start
            ": Call count: " + sch.getNumberOfCalls()
        );
        if (sch.getNumberOfCalls() == 60) {
            sch.stop(); //stop the scheduler
        }
    },
    100); //repeat every 100 milliseconds

Options

You can pass an options object as the Scheduler constructor's 3rd parameter:

    // sch is a Scheduler's instance
    var myOnStopHandler = function(sch) {
        console.log("on stop called");
    }

    new RealScheduler(callback, delay, {waitForTheFirstCall: false,
    onStop: myOnStopHandler});

Following optional parameters are possible:

  • waitForTheFirstCall {boolean} - If true, waits for delay milliseconds before the first callback execution. Defaults to true.
  • onStop {function} - A callback, called after a scheduler's stop() method is called. A function (sch) => {} where sch is an enclosing RealScheduler instance. Defaults to null.
  • onDeltaError {function} - A callback, called after a time difference greater than delay occured. A function (sch) => {} where sch is an enclosing RealScheduler instance. Defaults to null.

Dependents (0)

Package Sidebar

Install

npm i real-scheduler

Weekly Downloads

1

Version

0.0.8

License

MIT

Unpacked Size

15.3 kB

Total Files

8

Last publish

Collaborators

  • tomaskraus