Infinite loop for Node.js. Easy to Use & Good Performance
A helper for running tasks repeatly in Node.js.
get started by:
npm install infinite-loop
- require it
var InfiniteLoop = require('infinite-loop');
- create a new il
var il = new InfiniteLoop;
- add a task
//simple ++ counter example
var counter = 0;
//task you want to run infinitely
function addOne() {
counter++;
console.log(counter);
}
//add it by calling .add
il.add(addOne, []);
- run it
il.run();
the infinite-loop is also chainable
il.add(addOne, counter).run();
The output will be:
1
2
3
4
5
6
...
Find out more feature at the APIs section
Infinite Loop use setImediate
internally to run task repeatly.
.add(function, [arguments...])
.add
take one or more arguments.
The first one must be a function, the rest arguments are the function's arguments. If the first arguments is not a function , InfiniteLoop will throw an Error.
invoke the task
.run([times])
.run()
take one argument optionally. By setting the optional argument:times, the task will only run the exact times.
.setInterval(interval)
It will set an interval for the task.argument:interval should be a number and should >0
You should call .setInterval
before .run
.removeInterval()
remove interval
.onError(errHandler)
If not used properly, Infiniteloop will throws some error. By calling .onError
, you could catch these errors, and prevent the app from crashing.
argument:errHandler must be a function
example:
il.onError(function(error){
console.log(error);
});
you could emit custom errors by calling il.emit('error', new Error('error message'))
Stop the InfiniteLoop
example: stop the loop after 10 seconds
setTimeout( function(){
il.stop();
} , 10 * 1000);
by Spencer.Z