2017-04-16 65 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等,如果我们改变它在即时计时器事件将调用我们的新功能。