Browse Source

Add optional timestamp parameter (#27421)

Raoul v. R 1 year ago
parent
commit
e049f9c709
2 changed files with 14 additions and 7 deletions
  1. 12 5
      docs/examples/en/misc/Timer.html
  2. 2 2
      examples/jsm/misc/Timer.js

+ 12 - 5
docs/examples/en/misc/Timer.html

@@ -11,7 +11,7 @@
 		<h1>[name]</h1>
 		<h1>[name]</h1>
 
 
 		<p class="desc">
 		<p class="desc">
-			This class is an alternative to [page:Clock] with a different API design and behavior
+			This class is an alternative to [page:Clock] with a different API design and behavior.
 			The goal is to avoid the conceptual flaws that became apparent in [page:Clock] over time.
 			The goal is to avoid the conceptual flaws that became apparent in [page:Clock] over time.
 
 
 			<ul>
 			<ul>
@@ -37,11 +37,12 @@
 		<code>
 		<code>
 		const timer = new Timer();
 		const timer = new Timer();
 
 
-		function animate() {
+		function animate( timestamp ) {
 
 
 			requestAnimationFrame( animate );
 			requestAnimationFrame( animate );
-			
-			timer.update();
+
+			// timestamp is optional
+			timer.update( timestamp );
 
 
 			const delta = timer.getDelta();
 			const delta = timer.getDelta();
 
 
@@ -109,8 +110,14 @@
 			Sets a time scale that scales the time delta in [page:.update]().
 			Sets a time scale that scales the time delta in [page:.update]().
 		</p>
 		</p>
 
 
-		<h3>[method:this update]()</h3>
+		<h3>[method:this update]( [param:Number timestamp] )</h3>
 		<p>
 		<p>
+			timestamp -- (optional) The current time in milliseconds. Can be obtained from the
+			[link:https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame requestAnimationFrame]
+			callback argument. If not provided, the current time will be determined with
+			[link:https://developer.mozilla.org/en-US/docs/Web/API/Performance/now performance.now]. Please note that this
+			parameter has no effect when using a fixed time delta.<br /><br />
+
 			Updates the internal state of the timer. This method should be called once per simulation step
 			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 [page:.getDelta]()).
 			and before you perform queries against the timer (e.g. via [page:.getDelta]()).
 		</p>
 		</p>

+ 2 - 2
examples/jsm/misc/Timer.js

@@ -104,7 +104,7 @@ class Timer {
 
 
 	}
 	}
 
 
-	update() {
+	update( timestamp ) {
 
 
 		if ( this._useFixedDelta === true ) {
 		if ( this._useFixedDelta === true ) {
 
 
@@ -113,7 +113,7 @@ class Timer {
 		} else {
 		} else {
 
 
 			this._previousTime = this._currentTime;
 			this._previousTime = this._currentTime;
-			this._currentTime = now() - this._startTime;
+			this._currentTime = ( timestamp !== undefined ? timestamp : now() ) - this._startTime;
 
 
 			this._delta = this._currentTime - this._previousTime;
 			this._delta = this._currentTime - this._previousTime;