2017-04-16 8 views
0

実行中のタイマーのコールバック機能を変更することはできますか?私は時間をリセットせずにタイマーのコールバック関数を更新したい。タイマーコールバック関数を変更するNodeJS

考える:

let timerExample = timers.setInterval(function() { console.log("setTimeout: It's been one second!"); }, 10000); 

可能性:このような

timerExample.callBack = function() { console.log('some new callback') }; 

答えて

1

はい。

cb = function(){ 
    console.log("setTimeout: It's been one second!"); 
}; 


let timerExample = timers.setInterval(function() { cb(); }, 10000); 
// 
// If we change our cb to a new function it will be called in our timer 
// 
cb = function() { console.log('some new callback') }; 

私たちがやっていることは、当社のタイマー機能の中からコールバック関数のCBを呼び出していると我々はオンザフライそれを変更した場合ので、タイマーイベントは、私たちの新しい関数を呼び出します。