我正在构建一个小的随机序列比较器以学习 js 对象,目前我遇到了一个问题,如果我在我的 completeMessage() 中增加限制变量,这将在我的 compareSequences() 的 for 循环中自动运行在无限循环中,谁能告诉我我哪里可能出错了?
有问题的2种方法:
compareSequences: function() {
var instance = this;
var i = 0;
for( i; i <= limit; i++ ) {
console.log('Limit inside loop:', limit);
if( stacks.randomSequence[i] != stacks.userSequence[i] ) {
instance.errorMessage();
return;
}
if( i === limit ) {
instance.completeMessage();
}
console.log('Limit now', limit);
}
},
completeMessage: function() {
var instance = this;
alert('Well done you where right!');
limit = 5; //currently set as test, changing to ++ results in infinite loop
instance.selectors.startButton.removeAttr('disabled');
overallScore++;
instance.selectors.scoreCounter.html(overallScore);
},
链接到我的小提琴:http: //jsfiddle.net/FNd79/22/