Three.js Timer vs Clock Delta Demo (r183)

Side-by-side demo showing why r183 recommends `THREE.Timer` for frame-delta reads, especially when multiple systems request timing in the same animation frame.

Key topics: three.js timer, three.js clock, r183, delta time, animation loop, frame rate independent animation

Overview

Frame-rate independent animation means moving things by "units per second" rather than "units per frame", so a scene looks the same on a 60 Hz laptop and a 144 Hz gaming monitor. In Three.js that requires a reliable delta time each frame. Since r183 the library recommends THREE.Timer over the older THREE.Clock, and this demo shows exactly why by running both side by side.

How it works

Clock.getDelta() returns the time since the previous call to getDelta, not since the previous frame. That is fine when exactly one piece of code reads it, but real applications have several systems (animation mixers, physics, particles, camera controls) that each want the frame delta. The second reader in the same frame gets a value close to zero, so whatever it drives stutters or slows down.

Timer separates the two concerns. You call timer.update(timestamp) once at the top of the animation loop, passing the requestAnimationFrame timestamp, and every later timer.getDelta() or timer.getElapsed() call in that frame returns the same consistent value. In the demo the left cube is driven by a Clock that is read more than once per frame, and the right cube by a Timer; both are asked to rotate at the same radians-per-second rate.

Exploring the demo

Watch the two cubes: the Timer-driven cube turns at the intended speed while the Clock-driven cube lags because its later reads see almost no elapsed time. Switching browser tabs and coming back is also instructive, because Timer can be connected to the page visibility API so a long pause does not produce one huge delta.

Practical advice

Use one Timer per render loop, update it once per frame, and pass the delta down to subsystems instead of letting each one keep its own clock. Clamp very large deltas (for example to 1/30 s) before feeding them into physics so that a dropped frame does not tunnel objects through walls; the bouncing balls and gravity cubes demos rely on the same idea.

What this code does

- r183 Timing Update: `THREE.Timer` is designed for stable delta retrieval when multiple subsystems read timing in the same frame.
- Clock Pitfall: repeated `Clock.getDelta()` calls in one tick can produce inconsistent near-zero deltas for later reads.
- Side-by-Side Behavior: the left cube uses `Clock` with duplicate delta reads, while the right cube uses `Timer` with repeat-safe reads.
- Frame-Rate Independence: both cubes apply radians-per-second rotation, so speed is tied to elapsed time, not frame count.
- Migration Pattern: call `timer.update(timestamp)` once per animation frame, then read `timer.getDelta()` wherever needed.

Continue Learning

JavaScript Implementation

ES6+
import * as THREE from 'three'

const timer = new THREE.Timer()
timer.connect(document)
const clock = new THREE.Clock()

function animate (timestamp) {
  requestAnimationFrame(animate)

  // Old pattern can return inconsistent deltas if called multiple times
  clock.getDelta()
  const clockDelta = clock.getDelta()

  // Timer is stable within a frame after update()
  timer.update(timestamp)
  timer.getDelta()
  const timerDelta = timer.getDelta()

  clockMesh.rotation.y += 1.25 * clockDelta
  timerMesh.rotation.y += 1.25 * timerDelta

  renderer.render(scene, camera)
}

animate()

Related demos

Trending this week

Most popular