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簡單的能寫,深入的話,尤其是封裝的,看著一頭霧水?。?/p>
有時間我還是要好好看看那本js高級程序設計,多研究一下開源代碼!
添加回答
舉報