Skip to content

Large time delta values using Timer when page is not visible (in Firefox) #27626

@mrxz

Description

@mrxz

Description

One of the documented features of the Timer class is to avoid large time deltas when the app/page is inactive using the Page Visibility API. This works fine under Chrome, but using Firefox I still observed large delta times. It turns out that requestAnimationFrame callbacks are still being fired by Firefox even when the page is not visible, albeit at an exponentially increasing interval (1s, 2s, 4s, 8s, etc...).

Obviously there's little the Timer can do about requestAnimationFrame still taking place, but it might be worth considering:

  • Not letting any time elapse when it has detected that page is hidden (effectively guaranteeing delta = 0 for those "frames")
  • Exposing whether or not the Timer is "running" so the user can skip update logic without having to replicate the visibility tracking logic
  • Adding a note/warning to the Timer docs about this browser behaviour (not sure if other browsers beside Firefox do this)

One interesting example is webgl_morphtargets_sphere, which worked fine under Firefox, but actually regressed with the merge of #17912 where it switched to the Timer class.

Reproduction steps

  1. Setup a Timer as per the docs (https://threejs.org/docs/index.html?q=Timer#examples/en/misc/Timer)
  2. Log the delta time values
  3. Switch to another tab for several seconds and return
  4. Notice high delta times being logged (>=1s)

Code

const timer = new Timer();

function animate( timestamp ) {
	requestAnimationFrame( animate );

	timer.update();
	console.log(timer.getDelta()); // Reports >1s deltas under Firefox when page is hidden

	renderer.render( scene, camera );
}

Live example

The morphtargets_sphere example demonstrates the issue as it has been updated to use Timer instead of Clock + custom page visibility handling:

Screenshots

image

Version

r160

Device

Desktop

Browser

Firefox

OS

Linux

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions