我將setInterval函數存儲在一個數組中,並試圖稍後將它傳遞給一個新變量。將setInterval和回調傳遞給一個新變量
this.clock = setInterval(() => {
this.duration = this.timer.formatTime(this.userTime)
if (this.duration === '00:00:00') {
clearInterval(this.clock);
this.timer.reset();
}
}, 1)
尋找在setInterval的對象this.clock.callback
有Closure._this對象,但我不知道如何訪問它,因爲它是與<function scope>
領域。
有沒有辦法將setInterval函數的當前內容傳遞給一個新變量?
如...
this.newClock = this.clock
和this.newClock
將具有相同的倒計時時間爲this.clock
。
P.S.我新來張貼在stackoverflow。任何提出更好的問題提示或建議將不勝感激。
'setInterval'返回該間隔的間隔ID。你不能用它來獲得訪問功能。你可以做的是創建一個命名函數並告訴'setInterval'來調用它。 'function blah(){..}; setInterval(blah,1);' – IMTheNachoMan
@IMTheNachoMan發佈那個答案。 – Barmar
@Barmar:謝謝。抱歉回合。 – IMTheNachoMan