Reputation: 714
Is it possible to change the callback function of a running timer? I want to update what the callback function is for the timer without reseting the time.
Given:
let timerExample = timers.setInterval(function() { console.log("setTimeout: It's been one second!"); }, 10000);
Possible:
timerExample.callBack = function() { console.log('some new callback') };
Upvotes: 0
Views: 193
Reputation: 2008
Yes like this;
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') };
What we are doing is calling a callback function cb from within our timer function and so if we change it on-the-fly the timer event will call our new function.
Upvotes: 1