You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be nice if timers exported rearm() or at least a variant of it suitable for public consumption. One use case for this is to allow for easier and/or more efficient keepalive mechanisms where a timer needs to be reset after a packet is sent/received.
Currently the only two solutions to achieve this are to constantly start/clear a normal Timeout or use an Interval timer and have some extra logic in the Interval callback to determine whether you should actually execute the real callback (and even then you can lose accuracy). Being able to just rearm(timer) simplifies all of this greatly and has much less overhead than clearTimeout() followed by setTimeout() for every packet.
@mscdex Could you give some code to show how to use this feature? Assume function rearm() is already finished and you can use this function in your code.
@XadillaX I think what @mscdex suggests is something like this. Instead of having to write code such as
functionsomeAsyncWork(callback){console.log('Doing something which must not be done more than once per second');setTimeout(callback,50);}lettimeout;functiondoSomethingPeriodically(){someAsyncWork((err)=>{timeout=setTimeout(doSomethingPeriodically,1000);});}timeout=setTimeout(doSomethingPeriodically,1000);
it would be nice to just write
lettimeout;functiondoSomethingPeriodically(){someAsyncWork((err)=>{timeout.rearm();// or timers.rearm(timeout)});}timeout=setTimeout(doSomethingPeriodically,1000);
Activity
XadillaX commentedon May 22, 2017
@mscdex Could you give some code to show how to use this feature? Assume function
rearm()is already finished and you can use this function in your code.tniessen commentedon May 22, 2017
@XadillaX I think what @mscdex suggests is something like this. Instead of having to write code such as
it would be nice to just write
Fishrock123 commentedon May 22, 2017
Not sure I agree with exposing
rearm(), but the behavior you are looking for already exists: just calltimers.active()on the timer.Related to #11736
Fishrock123 commentedon May 24, 2017
@mscdex does that solve what you are looking for, or?
See https://github.com/nodejs/node/pull/11154/files#diff-e6ef024c3775d787c38487a6309e491dR240 for an example in practice.
mscdex commentedon May 24, 2017
@Fishrock123 I haven't tested yet, it may be.
targos commentedon Jan 21, 2018
should this stay open?
Fishrock123 commentedon Jan 22, 2018
My refresh PR solves this better, I think.
Certainly rearm() should not be used to refresh timers.
Fishrock123 commentedon Aug 7, 2018
Timeout#refresh()is now a public API in Node 10 (afaik) - see 46d335c