Skip to content

Commit e049f9c

Browse files
authored
Add optional timestamp parameter (#27421)
1 parent a2235b2 commit e049f9c

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

docs/examples/en/misc/Timer.html

+12-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<h1>[name]</h1>
1212

1313
<p class="desc">
14-
This class is an alternative to [page:Clock] with a different API design and behavior
14+
This class is an alternative to [page:Clock] with a different API design and behavior.
1515
The goal is to avoid the conceptual flaws that became apparent in [page:Clock] over time.
1616

1717
<ul>
@@ -37,11 +37,12 @@ <h2>Code Example</h2>
3737
<code>
3838
const timer = new Timer();
3939

40-
function animate() {
40+
function animate( timestamp ) {
4141

4242
requestAnimationFrame( animate );
43-
44-
timer.update();
43+
44+
// timestamp is optional
45+
timer.update( timestamp );
4546

4647
const delta = timer.getDelta();
4748

@@ -109,8 +110,14 @@ <h3>[method:this setTimescale]( [param:Number timescale] )</h3>
109110
Sets a time scale that scales the time delta in [page:.update]().
110111
</p>
111112

112-
<h3>[method:this update]()</h3>
113+
<h3>[method:this update]( [param:Number timestamp] )</h3>
113114
<p>
115+
timestamp -- (optional) The current time in milliseconds. Can be obtained from the
116+
[link:https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame requestAnimationFrame]
117+
callback argument. If not provided, the current time will be determined with
118+
[link:https://developer.mozilla.org/en-US/docs/Web/API/Performance/now performance.now]. Please note that this
119+
parameter has no effect when using a fixed time delta.<br /><br />
120+
114121
Updates the internal state of the timer. This method should be called once per simulation step
115122
and before you perform queries against the timer (e.g. via [page:.getDelta]()).
116123
</p>

examples/jsm/misc/Timer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class Timer {
104104

105105
}
106106

107-
update() {
107+
update( timestamp ) {
108108

109109
if ( this._useFixedDelta === true ) {
110110

@@ -113,7 +113,7 @@ class Timer {
113113
} else {
114114

115115
this._previousTime = this._currentTime;
116-
this._currentTime = now() - this._startTime;
116+
this._currentTime = ( timestamp !== undefined ? timestamp : now() ) - this._startTime;
117117

118118
this._delta = this._currentTime - this._previousTime;
119119

0 commit comments

Comments
 (0)