npm install intelli-timerIntelli-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.
- node >= 4.4.1
``bash`
npm install intelli-timer
`js`
var timer = require('intelli-timer');
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__
`js
timer.timing(function(timerCallback){
do_something(err, function(){
timerCallback(); // timerCallback() after finish do_something()
});
}, function(cost_time){
console.log(cost_time);
});
`
---------------------------------------
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:msfunction(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__
`js
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 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__
`js
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'
});
``