-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
timers: expose rearm() #13144
Comments
@mscdex Could you give some code to show how to use this feature? Assume function |
@XadillaX I think what @mscdex suggests is something like this. Instead of having to write code such as function someAsyncWork(callback) {
console.log('Doing something which must not be done more than once per second');
setTimeout(callback, 50);
}
let timeout;
function doSomethingPeriodically() {
someAsyncWork((err) => {
timeout = setTimeout(doSomethingPeriodically, 1000);
});
}
timeout = setTimeout(doSomethingPeriodically, 1000); it would be nice to just write let timeout;
function doSomethingPeriodically() {
someAsyncWork((err) => {
timeout.rearm(); // or timers.rearm(timeout)
});
}
timeout = setTimeout(doSomethingPeriodically, 1000); |
Not sure I agree with exposing Related to #11736 |
@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. |
@Fishrock123 I haven't tested yet, it may be. |
should this stay open? |
My refresh PR solves this better, I think. Certainly rearm() should not be used to refresh timers. |
|
It would be nice if
timers
exportedrearm()
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 thanclearTimeout()
followed bysetTimeout()
for every packet.The text was updated successfully, but these errors were encountered: