Javascript-当方法调用自身时,this.increment不会增加

标签 javascript

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/

相关文章:

c# - Javascript 更新为 asp :HiddenField isnot detected server side in IE, 但适用于 Chrome

javascript - 按类名删除元素?

javascript - 从 javascript 使用 REST Oauth 2.0 API 的安全方法

javascript - Mac OS X 上的 Chrome 扩展 : Window popups disappear

javascript - 带有 Jsf 1.1 实现的 Ajax

javascript - 尝试使用 httpClient 和 Angular 8 从 api 显示单个项目时是否有修复?

javascript - 如何用条件打破 axios promise ?

javascript - jQuery DOM 实现

javascript - 加载所有图像(包括缓存图像)后的 jquery 事件?

javascript - AngularJS:数组和深度 $watch 不起作用