intelli-timer

1.1.2 • Public • Published

Intelli-Timer.js

Intelli-Timer provides some practical functions about time. All these functions assume you follow the Node.js convention of providing a single callback as the last argument of your asynchronous function.

Requirement

  • node >= 4.4.1

Install

npm install intelli-timer

Documentation

var timer = require('intelli-timer');

timing(function(timerCallback), [callback])

timing is used to calculate how much time spending on doing something.

Arguments

  • function(timerCallback) - put your task in this function. When the task is done, call timerCallback().
  • timerCallback() - call this function when the task is done.
  • callback(cost_time) - a callback which is called when task is finished. unit:ms

Examples

timer.timing(function(timerCallback){

    do_something(err, function(){
        timerCallback();    // timerCallback() after finish do_something()
    });

}, function(cost_time){

    console.log(cost_time);

});

countdown(timeout, function(timerCallback), [callback])

countdown is used to set a time limit for doing some task. If it spend too much time, terminate the task and show error message.

Arguments

  • timeout - time limit for doing the task. unit:ms
  • function(timerCallback) - put your task in this function. When the task is done, call timerCallback().
  • timerCallback() - call this function when the task is done.
  • callback(err) - a callback which is called when task is finished or terminated. If task spends too much time, it will callback with err.

Examples

timer.countdown(500, function(timerCallback){  // time limit is 0.5 second

    do_something(err, function(){
        timerCallback();    // timerCallback() after finish do_something()
    });

}, function(err){

    if(err) console.log(err);  // err is null when the task is completed in time
    else console.log('success');

});

repeat(interval, duration, task, [callback])

repeat is a function that repeat the same function in duration.

Arguments

  • interval - the interval that function is called.
  • duration - the repeated duration.
  • task() - a function will be repeated.
  • callback() - a callback will be called after finish repeating.

Examples

timer.repeat(100, 900, function(){  // interval is 100ms, duration is 900ms

    console.log('Hello World'); // print out 'Hello World' for nine or ten times

}, function(){

    console.log('finish');  // finish and print 'finish'

});

Package Sidebar

Install

npm i intelli-timer

Weekly Downloads

12

Version

1.1.2

License

ISC

Last publish

Collaborators

  • larry850806