我有一个用setIntervalcalled创建的变量cycle1。它是在setInterval一个原型函数内部创建的一个名为 的对象eGi,这与$_. 创建它后,它仍然会触发,但脚本的其余部分和控制台无法访问。当我尝试从另一个原型函数中清除此间隔时,没有任何反应。
eGi.prototype.startGame = function() {
//other code...
if (somethingOrOther) {
var somethingElse = confirm("start interval?");
if (somethingElse) {
this.cycle1 = setInterval($_.cycle,toMS(INTERVAL_SECONDS));
}
} else {
this.cycle1 = setInterval($_.cycle,toMS(INTERVAL_SECONDS));
}
};
然后当我尝试在另一个函数中停止它时
eGi.prototype.gameOver = function() {
clearInterval(this.cycle1);
//other code...
if (restart) {
$_.startGame();
} else {
$_.mainMenu();
}
};
它永远不会被清除,并且似乎在$_.startGame. 我什至无法使用实例变量从 Chrome 控制台访问$_.cycle1它eGi,egi.cycle1. 奇怪的是,这适用于访问属于我的eGi对象的任何其他变量:
var eGi = function(console,cDom,ctxt,game,devMode) {
$_ = this;
this.game = game;
this.cDom = cDom; //specifically, these objects control the canvas' DOM
this.ctxt = ctxt; //and the CanvasRenderingContext2D
}
eGi.prototype.mainMenu = function() {
this.ctxt.fillText("Hello",250,250); //this works just fine
//etc
};
为什么不清零?
完整的代码/游戏在这里。