From d658e644f59d2029d646accd96fa7abc1dd93e53 Mon Sep 17 00:00:00 2001 From: Jeremiah Senkpiel Date: Wed, 25 Apr 2018 12:45:34 -0400 Subject: [PATCH] timers: make timer.refresh() a public API Originally added in bb5575aa75fd3071724d5eccde39a3041e1af57a discussions such as https://github.com/nodejs/node/issues/20261 show the usefulness of this API to the Node.js ecosystem. PR-URL: https://github.com/nodejs/node/pull/20298 --- doc/api/timers.md | 15 +++++++++++++++ lib/internal/http2/core.js | 7 +++---- lib/internal/timers.js | 6 +++--- lib/net.js | 5 ++--- test/parallel/test-timers-refresh.js | 25 +++++++++++++++++++++---- 5 files changed, 44 insertions(+), 14 deletions(-) diff --git a/doc/api/timers.md b/doc/api/timers.md index 376b5312e5a41a..c573d2afdb9e21 100644 --- a/doc/api/timers.md +++ b/doc/api/timers.md @@ -74,6 +74,21 @@ When called, requests that the Node.js event loop *not* exit so long as the By default, all `Timeout` objects are "ref'ed", making it normally unnecessary to call `timeout.ref()` unless `timeout.unref()` had been called previously. +### timeout.refresh() + + +* Returns: {Timeout} a reference to `timeout` + +Sets the timer's start time to the current time, and reschedules the timer to +call its callback at the previously specified duration adjusted to the current +time. This is useful for refreshing a timer without allocating a new +JavaScript object. + +Using this on a timer that has already called its callback will reactivate the +timer. + ### timeout.unref()