setTimeout vs requestAnimationFrame — and why your animation runs twice as fast on an iPhone

You built a simple browser game with AI: a ball rolls across the screen. On your laptop — perfect. A friend opens it on a new iPhone or a gaming monitor — and the ball flies twice as fast. The game is broken, even though the code is the same.
Another case: an animation on setTimeout looks smooth, but every now and then it twitches. Not lagging — twitching.
Both problems come down to one choice: setTimeout or requestAnimationFrame. Let's see how they differ, and I'll tell you straight what to use.
Quick version: what each one does
setTimeout(fn, ms) — "call this function no earlier than N milliseconds from now". It's an alarm clock. It doesn't care what's happening on screen.
requestAnimationFrame(fn) (rAF for short) — "call this function right before the browser paints the next frame". Not an alarm clock — a spot in line for the painter. The browser decides when the frame is and calls you on time.
Both take a callback — a function to be called later. The difference is who decides when.
Comparing what matters
When it fires:
setTimeout— after the given time. The number is a minimum, not a promise: if the browser is busy, the call runs late.- rAF — right before a frame is painted. In sync with the screen.
How often:
setTimeout(fn, 16)— aims for 60 times a second but lives its own life. Sometimes it hits one frame twice, sometimes it skips a frame. That's the twitching.- rAF — as many times as the screen refreshes. On a regular monitor 60 times a second; on an iPhone with ProMotion or a gaming monitor, 120 or more.
In a background tab:
setTimeout— the browser slows it down, usually to once a second, and even less often in tabs hidden for a long time. But the code keeps running.- rAF — in a hidden tab it stops almost everywhere. Nobody's watching, nothing to paint. Saves battery.
What it's for:
setTimeout— for logic over time: hide a toast after 3 seconds, retry a request, delay an action.- rAF — for anything that changes every frame: movement in a game, a canvas chart, a counter smoothly running up to a number.
The answer: why the ball flies twice as fast
Typical AI code:
function tick() {
ball.x += 2; // 2 pixels per frame
requestAnimationFrame(tick);
}
requestAnimationFrame(tick);
On a 60 Hz screen that's 120 pixels per second. On a 120 Hz screen tick runs twice as often — 240 pixels per second. The speed is tied to frames, not time.
The fix: count how much time has passed. rAF passes the current time into your function:
let last = performance.now();
function tick(now) {
const dt = (now - last) / 1000; // seconds since the last frame
last = now;
ball.x += 120 * dt; // 120 pixels per SECOND
requestAnimationFrame(tick);
}
requestAnimationFrame(tick);
Now the ball moves the same on any screen. On 120 Hz it just moves more smoothly — in smaller steps.
Want to see the difference with your own eyes? Add console.log(dt) inside tick. On a regular monitor you'll see about 0.0167 (one sixtieth of a second). On a 120 Hz screen, about 0.0083 — half that. That's why "per frame" code behaves differently on different devices: the number of frames per second simply differs.
Verdict: who should use what
- Making a game, canvas, a smooth counter, scroll-driven animation — only
requestAnimationFrame. And computedtfrom day one, or everything speeds up on fast screens. - Need "do something in N seconds" —
setTimeout. rAF won't help here: it's about frames, not seconds. - Repeating an action once a minute (checking for new messages) —
setIntervalor a loopingsetTimeout. Remember it slows down in the background. - A simple button animation or a block fading in — neither. Use a CSS
transition. The browser makes it smooth with zero lines of JavaScript and doesn't touch the DOM more than needed.
The classic beginner mistake is animating with setTimeout(fn, 16). It almost works, which is exactly why it's hard to notice. If you see that number next to movement in AI-written code, ask for a rewrite with rAF and dt.
How do I stop requestAnimationFrame?
requestAnimationFrame returns an id. Pass it to cancelAnimationFrame(id) and the next frame won't be called. With setTimeout it's the same via clearTimeout.
Is setTimeout(fn, 0) instant?
No. The function runs after the current code, once the queue frees up. And with deeply nested timers the browser won't allow a delay under 4 ms.
Does rAF work in Node.js?
No, it's a browser function — Node.js has no frames. On the server, delays use setTimeout or setImmediate.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





