TimeModule Module

The TimeModule class enables time-based events.

Usage

const Time = require('Time');

Properties

PropertyDescription

ms

(get) ms: ScalarSignal (set) (Not Available)

Specifies a ScalarSignal indicating the number of milliseconds elapsed since the first frame.

Methods

MethodDescription

clearTimeout

clearTimeout(timer: Subscription): void

Cancels a callback set by setTimeout. This has no effect if the timeout has already been triggered. This function is provided as a to match the traditional JS clearTimeout API.

Note: This is equivalent to Subscription.unsubscribe.

setTimeout

setTimeout(callback: Function, delay: Number): Subscription

Returns a Subscription object. The function specified by callback is called after the specified delay in milliseconds. The callback is a function that has one argument, the elapsed time since the timer started.

Note: A timeout can be cancelled either via Subscription.unsubscribe or TimeModule.clearTimeout.

Note: This is equivalent to Time.ms.sub(Time.ms.pin()).trigger(delay).subscribe(callback).

Example

The following example demonstrates how to set a single-event timer, and a timer that is activated every 1.7 seconds.

const Time = require('Time');
const Diagnostics = require('Diagnostics');

// One-shot timers:
Time.setTimeout(
  function (elapsedTime) { 
    Diagnostics.log('You should see this message in the console.'); 
  }, 1300);

var cancelableTimer = Time.setTimeout(
  function (elapsedTime) { 
    Diagnostics.log('You should not see this message in the console.'); 
  }, 2900);

// Repeating timer:
Time.ms.interval(1700).subscribe(
  function (elapsedTime) {
    // NOTE: Time.ms may differ slightly from the elapsed
    // time passed to the callback. Time.ms shows the exact
    // time since the effect started, whereas the callback
    // exposes an exact multiple of the specified interval.
    Diagnostics.log(Time.ms.lastValue);
    Diagnostics.log(elapsedTime);
    Time.clearTimeout(cancelableTimer);
  });