Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timer: Add optional timestamp parameter. #27421

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions docs/examples/en/misc/Timer.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<h1>[name]</h1>

<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.

<ul>
Expand All @@ -37,11 +37,12 @@ <h2>Code Example</h2>
<code>
const timer = new Timer();

function animate() {
function animate( timestamp ) {

requestAnimationFrame( animate );

timer.update();

// timestamp is optional
timer.update( timestamp );

const delta = timer.getDelta();

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

<h3>[method:this update]()</h3>
<h3>[method:this update]( [param:Number timestamp] )</h3>
<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
and before you perform queries against the timer (e.g. via [page:.getDelta]()).
</p>
Expand Down
4 changes: 2 additions & 2 deletions examples/jsm/misc/Timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Timer {

}

update() {
update( timestamp ) {

if ( this._useFixedDelta === true ) {

Expand All @@ -113,7 +113,7 @@ class Timer {
} else {

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

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

Expand Down