Full answer
requestAnimationFrame calls your function before the next paint, usually once per display refresh. The refresh rate is not fixed: it is 60 Hz on many screens, 120 Hz or more on others, and it drops when the page is busy. An animation that moves 5 pixels per callback runs twice as fast on a 120 Hz screen.
The reliable method:
- Remember the timestamp of the first frame.
- In each callback, compute
progress = (timestamp - start) / duration, and clamp it to the range 0 to 1. - Compute the value from the progress, for example
distance * easing(progress). - Ask for another frame only while the progress is below 1.
- Write the exact final value when finished, so rounding does not leave the element at 299.7 pixels.
The timestamp passed to the callback is the same high resolution time base as performance.now(), and every callback in one frame receives the same value. Use it, and avoid calling Date.now() inside the loop.
A few rules for good behaviour: change transform and not left, keep the work in the callback small, and store the id returned by requestAnimationFrame so that you can call cancelAnimationFrame when the component goes away.
For simple effects, CSS transitions or the Web Animations API (element.animate) are usually better, because the browser can run them off the main thread. A manual loop is for effects that depend on logic, such as physics or scroll-linked work.
The test runs the animation for 200 milliseconds and checks that the final position is exactly 300 pixels and that more than one frame ran.
<div id="box" style="width:40px;height:40px;background:teal"></div>
<script>
window.frames = 0;
window.finished = new Promise((resolve) => {
const duration = 200, distance = 300;
let start = null;
function step(ts) {
if (start === null) start = ts;
const progress = Math.min((ts - start) / duration, 1);
box.style.transform = `translateX(${distance * progress}px)`;
frames++;
if (progress < 1) requestAnimationFrame(step);
else resolve(box.style.transform);
}
requestAnimationFrame(step);
});
</script>Run in headless Chrome 156 through Playwright. Two full runs gave the same results. Checked in headless Chrome: the final transform was exactly translateX(300px) and more than one frame ran. Behaviour on a 120 Hz screen was not tested, and the method does not depend on it.
Key points
- Compute progress from the timestamp, not from the number of frames.
- Keep calling requestAnimationFrame until progress reaches 1.
- Set the exact end value on the last frame.
- Cancel the frame request when the animation is no longer needed.
- Prefer CSS or element.animate when the effect is simple.
Common mistakes
- Moving a fixed number of pixels per frame.
- Using setInterval and assuming it matches the refresh rate.
- Forgetting to stop at the end, which leaves a loop running.
- Animating left or top and causing layout on every frame.
Follow-ups
- Why does a hidden tab not run these callbacks?
- How would you add an easing function?
- When would you choose element.animate instead?
Tips
- Mention the clamped progress value, which is the core idea.
- Say that the same code works on any refresh rate.