var events = require('events'),timers = require('timers');
var EventEmitter = require('events').EventEmitter,
util = require('util');
//class initi
var myt = function()
{
}
util.inherits(myt, EventEmitter);
myt.prototype.fnc = function(c)
{
console.log(c + ":send custom");this.emit('tick recv',c);
}
var Ticker = new myt();
Ticker.on('tick recv',function(c)
{
console.log(c + ':got custom');
})
Ticker.tick = function(c)
{
Ticker.fnc(c);
timers.setTimeout(Ticker.tick(c+1), 100);
}
Ticker.tick(0);
I get "RangeError: Maximum call stack size exceeded" error (and also the timeouts do not seem to be working"). There seems to be some basic error. Can anyone spot ?
You're calling Ticker.tick() unconditionally inside Ticker.tick(). I think you mean:
Ticker.tick = function(c)
{
Ticker.fnc(c);
timers.setTimeout(function() { Ticker.tick(c+1); }, 100);
}
The first argument to setTimeout() should be a function; in your code, you were passing the result of calling a function.
You should pass a function to setTimeout and pass the function's arguments as the third, fourth, etc.. arguments of setTimeout, like so:
Ticker.tick = function(c) {
Ticker.fnc(c);
timers.setTimeout(Ticker.tick, 100, c+1);
}