Giter Site home page Giter Site logo

infiniteloop's Introduction

infiniteLoop

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

Easy to use:

  1. require it
var InfiniteLoop = require('infinite-loop');
  1. create a new il
var il = new InfiniteLoop;
  1. 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, []);
  1. 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

Good Performance

Infinite Loop use setImediate internally to run task repeatly.

APIs

.add

.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.

.run

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

.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

.removeInterval() remove interval

.onError

.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

Stop the InfiniteLoop

example: stop the loop after 10 seconds

setTimeout( function(){
    il.stop();
    } , 10 * 1000);

by Spencer.Z

infiniteloop's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

infiniteloop's Issues

"timeout id undefined" message if stop method called

When I run the following code and call stop method, the infinite loop not stopped and continously output timeout id undefined error message.

var InfiniteLoop = require('infinite-loop');

function Test() {
    this.connected = false;
}

Test.prototype.connect = function () {
    var self = this;
    var il = new InfiniteLoop();

    this.connected = true;
    il.add(function () {
        if (self.connected) {
            // Do something
            console.log('a');
        } else {
            il.stop();
        }
    });
    il.run();
};

Test.prototype.disconnect = function () {
    this.connected = false;
};

var test = new Test();
test.connect();

setTimeout(function () {
    test.disconnect();
}, 1000);

Output:

a
a
a
...
a
a
timeout id undefined <- test.disconnect(); called
timeout id undefined
timeout id undefined
timeout id undefined
timeout id undefined <- repeat continously
...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.