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: only update delta and elapsed time when document is visible #27651

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions examples/jsm/misc/Timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,20 @@ class Timer {

update( timestamp ) {

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

this._delta = ( this._currentTime - this._previousTime ) * this._timescale;
this._elapsed += this._delta; // _elapsed is the accumulation of all previous deltas
if ( this._usePageVisibilityAPI === true && document.hidden ) {

this._delta = 0;

} else {

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

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

}

return this;

Expand Down