uloop. timer

Represents a uloop timer instance as returned by timer().

Example
const timeout = uloop.timer(…);

timeout.set(…);
timeout.remaining();
timeout.cancel();

Methods

cancel() → {boolean}

Cancels the uloop timer, disarming it and removing it from the event loop.

This method destroys the uloop timer and releases its associated resources. Calling it on a timer that already fired is not required; the timer then only drops its callback and returns false.

Returns: boolean

Returns true on success.

Example
// Cancel the uloop timer
timer.cancel();

remaining() → {number}

Returns the number of milliseconds until the uloop timer expires.

This method returns the remaining time until the uloop timer expires. If the timer is not armed (i.e., disabled), it returns -1.

Returns: number

The number of milliseconds until the timer expires, or -1 if the timer is not armed.

Example
// Get the remaining time until the uloop timer expires (~500ms)
const remainingTime = timer.remaining();
if (remainingTime !== -1)
    printf("Time remaining until timer expires: %d ms\n", remainingTime);
else
    printf("Timer is not armed\n");

set(timeoutopt, nullable) → {boolean}nullable

Rearms the uloop timer with the specified timeout.

This method rearms the uloop timer with the specified timeout value, allowing it to trigger after the specified amount of time. A zero or negative timeout expires the timer on the next event loop iteration. If no timeout value or null is provided, the timer is disarmed and keeps its callback, so that a later call can rearm it.

Parameters:
NameTypeDescription
timeoutnumber(optional, nullable)

Optional. The timeout value in milliseconds until the timer expires. Without a value, the timer is disarmed.

Returns: boolean

Returns true on success, null on error, such as an invalid timeout argument.

Example
const timeout = uloop.timer(…);

// Rearm the uloop timer with a timeout of 1000 milliseconds
timeout.set(1000);

// Disable the uloop timer
timeout.set();