var CHECK_ITERATIONS = 5;
function NewCheckEvent(device, account, eventId, url) {
this.iteration = 0;
this.status = "checking";
this.device = device;
this.url = url;
}
NewCheckEvent.prototype.runCheckLoop = function() {
if (this.iteration < CHECK_ITERATIONS) {
this.iteration = this.iteration++;
console.log('iteration ' + this.iteration);
setTimeout(function (){
this.runCheckLoop();
}.bind(this), 1000);
}
}
var bob = new NewCheckEvent(14245, 92136, "9f2b2d3f-45d4-a82f-b12e082b1164", "dddd");
console.log(bob);
bob.runCheckLoop();
one@localhost ~ $ node try.js
{ iteration: 0, status: 'checking', device: 14245, url: 'dddd' }
iteration 0
iteration 0
iteration 0
当runCheckLoop
在setTimeout中调用自身时,怎么会没有增加this.iteration?我该如何解决这个问题?
答案 0 :(得分:4)
this.iteration = this.iteration++;
this.iteration++
递增属性,但随后返回原始值。左侧的赋值将其设置回原始值。也许你只想
this.iteration++;
答案 1 :(得分:3)
行中的问题
this.iteration = this.iteration++;
这里你实际上说:设置this.iteration = this.iteration然后递增;因此,您将旧值分配回来。它应该只是
this.iteration++;
代替。