我的代码出了点问题。我试图在新的锦标赛()上尝试在对象构造函数中启动间隔调用Prototype函数。但是node.js抛出错误:RangeError:超出了最大调用堆栈大小。
当我没有向计时器对象分配计时器时,一切都很好。 var timer = setInterval();效果也不错。
你能解释一下吗?
var Tournament = function() {
var tournament = this;
console.log("Creating new instance.");
this.users = [];
this.state = 'waitingForPlayers';
this._id = generateId();
this.startTimer(6, tournament.startTournament);
};
Tournament.prototype.startTimer = function(time, timoutCallback) {
var tournament = this;
tournament.time = time;
tournament.timer = setInterval(function() {
tournament.time--;
console.log(Tournament.time);
if(tournament.time <= 0) {
clearInterval(tournament.timer);
timoutCallback();
};
}, 1000);
};
Tournament.prototype.startTournament = function() {
console.log("Tournament is starting.");
};
Edited:
I'm creating instance:
var tournament = new Tournament();
game.instances.push(tournament);