Skip to content

Commit 08b24a1

Browse files
authored
Timer: Added FixedTimer (#27423)
* Timer: Added FixedTimer. * Updated docs. * Updated docs.
1 parent f163ee9 commit 08b24a1

File tree

2 files changed

+32
-79
lines changed

2 files changed

+32
-79
lines changed

docs/examples/en/misc/Timer.html

+5-27
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ <h1>[name]</h1>
1717
<ul>
1818
<li>[name] has an [page:.update]() method that updates its internal state. That makes it possible to call [page:.getDelta]() and [page:.getElapsed]() multiple times per simulation step without getting different values.</li>
1919
<li>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).</li>
20-
<li>It's possible to configure a fixed time delta and a time scale value (similar to Unity's Time interface).</li>
2120
</ul>
2221
</p>
2322

@@ -65,21 +64,6 @@ <h3>Timer()</h3>
6564

6665
<h2>Methods</h2>
6766

68-
<h3>[method:this disableFixedDelta]()</h3>
69-
<p>
70-
Disables the usage of a fixed delta time.
71-
</p>
72-
73-
<h3>[method:this dispose]()</h3>
74-
<p>
75-
Can be used to free all internal resources. Usually called when the timer instance isn't required anymore.
76-
</p>
77-
78-
<h3>[method:this enableFixedDelta]()</h3>
79-
<p>
80-
Enables the usage of a fixed delta time.
81-
</p>
82-
8367
<h3>[method:Number getDelta]()</h3>
8468
<p>
8569
Returns the time delta in seconds.
@@ -90,33 +74,27 @@ <h3>[method:Number getElapsed]()</h3>
9074
Returns the elapsed time in seconds.
9175
</p>
9276

93-
<h3>[method:Number getFixedDelta]()</h3>
77+
<h3>[method:this setTimescale]( [param:Number timescale] )</h3>
9478
<p>
95-
Returns the fixed time delta that has been previously configured via [page:.setFixedDelta]().
79+
Sets a time scale that scales the time delta in [page:.update]().
9680
</p>
9781

9882
<h3>[method:this reset]()</h3>
9983
<p>
10084
Resets the time computation for the current simulation step.
10185
</p>
10286

103-
<h3>[method:this setFixedDelta]( [param:Number delta] )</h3>
104-
<p>
105-
Defines a fixed time delta value which is used to update the timer per simulation step.
106-
</p>
107-
108-
<h3>[method:this setTimescale]( [param:Number timescale] )</h3>
87+
<h3>[method:this dispose]()</h3>
10988
<p>
110-
Sets a time scale that scales the time delta in [page:.update]().
89+
Can be used to free all internal resources. Usually called when the timer instance isn't required anymore.
11190
</p>
11291

11392
<h3>[method:this update]( [param:Number timestamp] )</h3>
11493
<p>
11594
timestamp -- (optional) The current time in milliseconds. Can be obtained from the
11695
[link:https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame requestAnimationFrame]
11796
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 />
97+
[link:https://developer.mozilla.org/en-US/docs/Web/API/Performance/now performance.now].<br /><br />
12098

12199
Updates the internal state of the timer. This method should be called once per simulation step
122100
and before you perform queries against the timer (e.g. via [page:.getDelta]()).

examples/jsm/misc/Timer.js

+27-52
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ class Timer {
1111

1212
this._timescale = 1;
1313

14-
this._useFixedDelta = false;
15-
this._fixedDelta = 16.67; // ms, corresponds to approx. 60 FPS
16-
1714
// use Page Visibility API to avoid large time delta values
1815

1916
this._usePageVisibilityAPI = ( typeof document !== 'undefined' && document.hidden !== undefined );
@@ -28,34 +25,6 @@ class Timer {
2825

2926
}
3027

31-
disableFixedDelta() {
32-
33-
this._useFixedDelta = false;
34-
35-
return this;
36-
37-
}
38-
39-
dispose() {
40-
41-
if ( this._usePageVisibilityAPI === true ) {
42-
43-
document.removeEventListener( 'visibilitychange', this._pageVisibilityHandler );
44-
45-
}
46-
47-
return this;
48-
49-
}
50-
51-
enableFixedDelta() {
52-
53-
this._useFixedDelta = true;
54-
55-
return this;
56-
57-
}
58-
5928
getDelta() {
6029

6130
return this._delta / 1000;
@@ -68,15 +37,17 @@ class Timer {
6837

6938
}
7039

71-
getFixedDelta() {
40+
getTimescale() {
7241

73-
return this._fixedDelta / 1000;
42+
return this._timescale;
7443

7544
}
7645

77-
getTimescale() {
46+
setTimescale( timescale ) {
7847

79-
return this._timescale;
48+
this._timescale = timescale;
49+
50+
return this;
8051

8152
}
8253

@@ -88,40 +59,44 @@ class Timer {
8859

8960
}
9061

91-
setFixedDelta( fixedDelta ) {
62+
dispose() {
63+
64+
if ( this._usePageVisibilityAPI === true ) {
9265

93-
this._fixedDelta = fixedDelta * 1000;
66+
document.removeEventListener( 'visibilitychange', this._pageVisibilityHandler );
67+
68+
}
9469

9570
return this;
9671

9772
}
9873

99-
setTimescale( timescale ) {
74+
update( timestamp ) {
10075

101-
this._timescale = timescale;
76+
this._previousTime = this._currentTime;
77+
this._currentTime = ( timestamp !== undefined ? timestamp : now() ) - this._startTime;
78+
79+
this._delta = ( this._currentTime - this._previousTime ) * this._timescale;
80+
this._elapsed += this._delta; // _elapsed is the accumulation of all previous deltas
10281

10382
return this;
10483

10584
}
10685

107-
update( timestamp ) {
108-
109-
if ( this._useFixedDelta === true ) {
110-
111-
this._delta = this._fixedDelta;
86+
}
11287

113-
} else {
88+
class FixedTimer extends Timer {
11489

115-
this._previousTime = this._currentTime;
116-
this._currentTime = ( timestamp !== undefined ? timestamp : now() ) - this._startTime;
90+
constructor( fps = 60 ) {
11791

118-
this._delta = this._currentTime - this._previousTime;
92+
super();
93+
this._delta = ( 1 / fps ) * 1000;
11994

120-
}
95+
}
12196

122-
this._delta *= this._timescale;
97+
update() {
12398

124-
this._elapsed += this._delta; // _elapsed is the accumulation of all previous deltas
99+
this._elapsed += ( this._delta * this._timescale ); // _elapsed is the accumulation of all previous deltas
125100

126101
return this;
127102

@@ -141,4 +116,4 @@ function handleVisibilityChange() {
141116

142117
}
143118

144-
export { Timer };
119+
export { Timer, FixedTimer };

0 commit comments

Comments
 (0)