Timer
This class is an alternative to Clock with a different API design and behavior. The goal is to avoid the conceptual flaws that became apparent in Clock over time.
- Timer has an .update() method that updates its internal state. That makes it possible to call .getDelta() and .getElapsed() multiple times per simulation step without getting different values.
- The class uses the Page Visibility API to avoid large time delta values when the app is inactive (e.g. tab switched or browser hidden).
Import
Timer is an add-on, and must be imported explicitly. See Installation / Addons.
import { Timer } from ‘three/addons/misc/Timer.js’;
Code Example
const timer = new Timer();
function animate( timestamp ) {
requestAnimationFrame( animate );
// timestamp is optional
timer.update( timestamp );
const delta = timer.getDelta();
// do something with delta
renderer.render( scene, camera );
}
Constructor
Timer()
Methods
.getDelta () : Number
Returns the time delta in seconds.
.getElapsed () : Number
Returns the elapsed time in seconds.
.setTimescale ( timescale : Number ) : this
Sets a time scale that scales the time delta in .update().
.reset () : this
Resets the time computation for the current simulation step.
.dispose () : this
Can be used to free all internal resources. Usually called when the timer instance isn’t required anymore.
.update ( timestamp : Number ) : this
timestamp — (optional) The current time in milliseconds. Can be obtained from the requestAnimationFrame callback argument. If not provided, the current time will be determined with performance.now.
Updates the internal state of the timer. This method should be called once per simulation step and before you perform queries against the timer (e.g. via .getDelta()).