uloop. interval

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

Example
const intv = uloop.interval(…);

intv.set(…);
intv.remaining();
intv.expirations();
intv.cancel();

Methods

cancel() → {boolean}

Cancels the uloop interval.

This method cancels the uloop interval, disarming it and removing it from the event loop. Associated resources are released.

Returns: boolean

Returns true on success.

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

expirations() → {number}

Returns number of times the interval timer fired.

This method returns the number of times the uloop interval timer has expired (fired) since it was instantiated.

Returns: number

The number of times the uloop interval timer has expired (fired).

Example
// Get the number of times the uloop interval timer has expired
const expirations = interval.expirations();
printf("Number of expirations: %d\n", expirations);

remaining() → {number}

Returns the milliseconds until the next expiration.

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

Returns: number

The milliseconds until the next expiration of the uloop interval, or -1 if the interval is not armed.

Example
// Get the milliseconds until the next expiration of the uloop interval
const remainingTime = interval.remaining();

if (remainingTime !== -1)
    printf("Milliseconds until next expiration: %d\n", remainingTime);
else
    printf("Interval is not armed\n");

set(intervalopt) → {boolean}nullable

Rearms the uloop interval timer with the specified interval.

This method rearms the interval timer with the specified interval value, allowing it to trigger repeatedly after the specified amount of time. If no interval value is provided or if the provided value is negative, the interval remains disabled until rearmed with a positive interval value.

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

Optional. The interval value in milliseconds specifying when the interval triggers again. Defaults to -1, which disables the interval until rearmed with a positive interval value.

Returns: boolean

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

Example
// Rearm the uloop interval with a interval of 1000 milliseconds
const success = interval.set(1000);

if (success)
    printf("Interval rearmed successfully\n");
else
    printf("Error occurred while rearming interval: ${uloop.error()}\n");

// Disable the uloop interval
const success = interval.set();

if (success)
    printf("Interval disabled successfully\n");
else
    printf("Error occurred while disabling interval: ${uloop.error()}\n");