2017-04-16 66 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等,如果我們改變它在即時計時器事件將調用我們的新功能。