The TimeModule
class enables time-based events.
//=========================================================================== // The following example demonstrates how to call a function repeatedly every // 0.5 seconds as well as call a function once after 5 seconds has passed. // // Project setup: // - Insert a plane // - Insert a cube 3D primitive from the Asset Library //=========================================================================== // Load the required modules const Scene = require('Scene'); const Time = require('Time'); const Reactive = require('Reactive'); (async function() { // Enable async/await in JS [part 1] // Locate the plane in the Scene const [plane] = await Promise.all([ Scene.root.findFirst('plane0') ]); // Create a variable used in the timers const timeInMilliseconds = 500; //========================================================================== // Increase and decrease the scale of the plane every 0.5 seconds //========================================================================== // Create a boolean to determine if the plane's width is doubled or not var isPlaneDoubleWidth = false; // Store a reference to the plane's transform signal const planeTransform = plane.transform; // Store a reference to the plane's initial x-axis scale value const planeWidth = planeTransform.scaleX.pinLastValue(); // Create a function that changes the width of the plane function changePlaneWidth() { // If the plane's width is not doubled... if (!isPlaneDoubleWidth) { // Multiply the x-axis scale value of the plane by 2, doubling it planeTransform.scaleX = Reactive.mul(planeWidth, 2); // Otherwise... } else { // Set the x-axis scale back to it's original value, halving it planeTransform.scaleX = planeWidth; } // Update the boolean isPlaneDoubleWidth = !isPlaneDoubleWidth; } // Create an interval timer that calls the changePlaneWidth function every 0.5 // seconds const intervalTimer = Time.setInterval(changePlaneWidth, timeInMilliseconds); //========================================================================== // Stop the interval timer after 5 seconds using a timeout timer //========================================================================== // Create a function that stops the interval timer function stopIntervalTimer() { Time.clearInterval(intervalTimer); } // Create a timeout timer that calls the stopIntervalTimer function after 5 // seconds have passed const timeoutTimer = Time.setTimeout(stopIntervalTimer, timeInMilliseconds * 10); //========================================================================== // Animate an object along its forward vector with delta time //========================================================================== // Locate the cube object in the scene const cube = await Scene.root.findFirst('Cube'); // Create a new vector const vector = Reactive.vector(0, 0, -1); // Rotate the cube by the cube's rotation vector const rotatedVector = vector.rotate(cube.transform.rotation); // Set the velocity using delta time const velocity = Time.deltaTimeMS.div(1000).mul(rotatedVector); // Animate the cube along its forward vector at the velocity specified cube.transform.position = cube.transform.position.history(1).frame(-1).add(velocity); })(); // Enable async/await in JS [part 2]
Property | Description |
---|---|
deltaTimeMS | (get) deltaTimeMS: ScalarSignal Retrieves delta time information in milliseconds from the effect. You can use the delta time value to create animations which transition independently of the frame rate. See the main example on the page. |
ms | (get) ms: ScalarSignal Specifies a ScalarSignal indicating the number of milliseconds elapsed since the first frame. |
Method | Description |
---|---|
clearInterval | clearInterval(subscription: Subscription): void Cancels a callback set by setInterval . This function is provided as a to match the traditional JS clearInterval API.Note: This is equivalent to Subscription.unsubscribe . |
clearTimeout | clearTimeout(subscription: 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 . |
setInterval | setInterval(callback: {}, delay: number): Subscription Returns a Subscription object. The function specified by callback is called at intervals specified by delay in milliseconds. The setInterval will continue calling the callback until TimeModule.clearInterval is called. The callback is a function that has one argument, the elapsed time since the timer started. An exception is thrown when the delay is zero or less.Note: An interval can be canceled either via Subscription.unsubscribe or TimeModule.clearInterval . |
setIntervalWithSnapshot | setIntervalWithSnapshot(snapshot: {[name: string]: BoolSignal | ScalarSignal | StringSignal}, callback: {}, delay: number): Subscription Returns a Subscription object.The function specified by callback is called at intervals specified by delay in milliseconds using Snapshot of signals.Snapshot is a dictionary of String/Bool/Scalar signals, which will be passed as JSON to the callback function using lastValue from requested signals.The setIntervalWithSnapshot will continue calling the callback until TimeModule.clearInterval is called.The callback is a function that has two arguments, the elapsed time since the timer started, and the snapshot JSONAn exception is thrown when the delay is zero or less.Note: An interval can be canceled either via Subscription.unsubscribe or TimeModule.clearInterval . |
setTimeout | setTimeout(callback: {}, 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 canceled either via Subscription.unsubscribe or TimeModule.clearTimeout .Note: This is equivalent to Time.ms.sub(Time.ms.pin()).trigger(delay).subscribe(callback) . |
setTimeoutWithSnapshot | setTimeoutWithSnapshot(snapshot: {[name: string]: BoolSignal | ScalarSignal | StringSignal}, callback: {}, delay: number): Subscription Returns a Subscription object. The function specified by callback is called after the specified delay in milliseconds using Snapshot of signals.Snapshot is a dictionary of String/Bool/Scalar signals, which will be passed as JSON to the callback function using lastValue from requested signals.The callback is a function that has two arguments, the elapsed time since the timer started and the snapshot JSON.Note: A timeout can be canceled either via Subscription.unsubscribe or TimeModule.clearTimeout .Note: This is equivalent to Time.ms.sub(Time.ms.pin()).trigger(delay).subscribeWithSnapshot(snapshot, callback) . |
This module exposes no classes.