3 回答

TA贡献1839条经验 获得超15个赞
Start: function () { //console.log("Start totalIntervalMs:" + this.totalIntervalMs); this.timer = setInterval( () => { this.IntervalHandle(); }, this.intervalMs); //console.log("Start timer:" + this.timer); },
这里改成这样就可以了

TA贡献1848条经验 获得超2个赞
楼上的解答是正确的,但这是ES6的写法,其实你的问题是一个“js闭包”的问题而已。
解决办法可以先声明一个全局变量
步骤一
var this_ = null;
步骤二
Start: function () {
//console.log("Start totalIntervalMs:" + this.totalIntervalMs);
this_ = this;
this.timer = setInterval( this.IntervalHandle , this.intervalMs);
/****************************
说明,问题是你原来是this.IntervalHandle()调用的,但实际上应该是this.IntervalHandle,不需要();
问题又来了,this.IntervalHandle在setInterval里面单独调用的话那么this.IntervalHandle函数的
this指向的就是windos对象了。因为setInterval是全局函数。
***********************************************/
//console.log("Start timer:" + this.timer);
},
步骤三
IntervalHandle: function () {
this_.totalIntervalMs += this_.intervalMs ;
//console.log("IntervalHandle totalIntervalMs:" + this.totalIntervalMs);
this_.Oninterval(this_.totalIntervalMs);
//console.log("IntervalHandle timer:" + this.timer);
}

TA贡献1805条经验 获得超10个赞
谢谢,没太看明白,对于闭包一直没理解,js简单的能写,深入的话,尤其是封装的,看着一头雾水啊!
有时间我还是要好好看看那本js高级程序设计,多研究一下开源代码!
添加回答
举报