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.

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) → {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. If no timeout value is provided or if the provided value is negative, the timer remains disabled until rearmed with a positive timeout value.

Parameters:
NameTypeDescription
timeoutnumber(optional, default: -1)

Optional. The timeout value in milliseconds until the timer expires. Defaults to -1, which disables the timer until rearmed with a positive timeout.

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();