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 不增加?我该如何解决这个问题?
最佳答案
this.iteration = this.iteration++;
this.iteration++
递增属性,但随后返回原始值。左侧的赋值将其设置回原始值。也许你想要的只是
this.iteration++;
关于Javascript-当方法调用自身时,this.increment不会增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25253101/