🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
JS MASTER CLASS /// MASTER THE ENGINE /// BUILD LOGIC /// ASYNC PATTERNS /// JS MASTER CLASS /// MASTER THE ENGINE ///

requestAnimationFrame | JavaScript Tutorial - In-Depth Guide

Master requestAnimationFrame: how it differs from setTimeout/setInterval, building a smooth animation loop, the auto-pause behavior on hidden tabs, and combining it with delta time for frame-rate-independent motion.

Total XP: 0|💻 javascript XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

System Hub

Core logic.

Quick Quiz //

Does a requestAnimationFrame-based loop automatically pause when its tab is hidden/backgrounded?


🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

requestAnimationFrame schedules a callback to run right before the browser's next repaint, making it the correct tool for any JavaScript-driven visual animation — smoother and more efficient than setTimeout or setInterval.

1requestAnimationFrame | JavaScript Tutorial - In-Depth Guide Part 1

requestAnimationFrame(callback) schedules the callback to run right before the browser's next repaint — typically about 60 times per second on standard displays.

+
function animate() {
  moveElement();
  requestAnimationFrame(animate); // schedule the next frame
}
requestAnimationFrame(animate); // kick off the loop
localhost:3000
🎬

Syncing with the Browser

2requestAnimationFrame | JavaScript Tutorial - In-Depth Guide Part 2

Unlike setInterval, which keeps firing even on a hidden/backgrounded tab (wasting battery and CPU), requestAnimationFrame automatically pauses when the tab isn't visible.

+
// setInterval keeps running in a hidden tab, wasting resources
// requestAnimationFrame automatically pauses — no extra code needed
localhost:3000

Auto-Pauses on Hidden Tabs

3requestAnimationFrame | JavaScript Tutorial - In-Depth Guide Part 3

The callback receives a high-resolution timestamp argument, useful for calculating exactly how much time has passed since the last frame.

+
let lastTime = 0;
function animate(timestamp) {
  const delta = timestamp - lastTime;
  lastTime = timestamp;
  updatePosition(delta);
  requestAnimationFrame(animate);
}
localhost:3000

The Timestamp Argument

4requestAnimationFrame | JavaScript Tutorial - In-Depth Guide Part 4

Using the delta time between frames (rather than a fixed step) keeps animation speed consistent even if the actual frame rate varies, like dropping to 30fps under heavy load.

+
function animate(timestamp) {
  const delta = timestamp - lastTime;
  lastTime = timestamp;
  position += speed * (delta / 1000); // pixels per second, frame-rate independent
  requestAnimationFrame(animate);
}
localhost:3000

Frame-Rate-Independent Motion

5requestAnimationFrame | JavaScript Tutorial - In-Depth Guide Part 5

cancelAnimationFrame(id) stops a scheduled animation frame callback — essential for cleaning up an animation loop when it's no longer needed, like on component unmount.

+
let frameId;
function animate() {
  update();
  frameId = requestAnimationFrame(animate);
}
frameId = requestAnimationFrame(animate);
// later, on cleanup:
cancelAnimationFrame(frameId);
localhost:3000

Cancelling with cancelAnimationFrame

6Step-by-Step Breakdown

requestAnimationFrame(callback) schedules the callback to run right before the browser's next repaint — typically about 60 times per second on standard displays.

Unlike setInterval, which keeps firing even on a hidden/backgrounded tab (wasting battery and CPU), requestAnimationFrame automatically pauses when the tab isn't visible.

Checkpoint: Does a requestAnimationFrame-based loop automatically pause when its tab is hidden/backgrounded?

  • Yes, automatically, with no extra code needed
  • No, it keeps running just like setInterval

The callback receives a high-resolution timestamp argument, useful for calculating exactly how much time has passed since the last frame.

Using the delta time between frames (rather than a fixed step) keeps animation speed consistent even if the actual frame rate varies, like dropping to 30fps under heavy load.

Checkpoint: Why is using delta time (rather than a fixed step) important for animation speed?

  • It keeps motion speed consistent regardless of the actual frame rate
  • It has no effect on animation speed at all

cancelAnimationFrame(id) stops a scheduled animation frame callback — essential for cleaning up an animation loop when it's no longer needed, like on component unmount.

Next, we'll explore 'The Event Loop: Microtask/Macrotask Ordering'.

Level Up 🚀

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1Respect prefers-reduced-motion in requestAnimationFrame-Driven Animations

Check `window.matchMedia('(prefers-reduced-motion: reduce)')` before starting a requestAnimationFrame animation loop, and skip or drastically simplify motion effects for users who have indicated a preference for reduced motion, to avoid discomfort or vestibular issues.

SEO Implications

  • 1

    Efficient Animations Improve Interaction Responsiveness Metrics

    Using requestAnimationFrame instead of less efficient timer-based animation loops helps maintain a responsive main thread, contributing positively to Interaction to Next Paint, a Core Web Vital influencing search ranking.

Best Practices

Always Use requestAnimationFrame (Not setTimeout/setInterval) for Visual Animations

It's synced to the display's actual repaint cycle, automatically pauses on hidden tabs, and produces smoother results than an arbitrary timer interval.

Use Delta Time for Any Animation Whose Speed Matters

Frame-rate-independent motion (using elapsed time rather than a fixed per-frame increment) ensures consistent visual speed across devices and under varying load.

Frequent Bugs

THE BUG

Building an animation loop with setInterval that keeps running (and draining battery) even when the user switches to a different browser tab.

THE FIX

Switch to requestAnimationFrame, which automatically pauses when the tab isn't visible, with no manual visibility-change handling required.

THE BUG

Moving an animated element by a fixed pixel amount per frame, causing it to visibly speed up or slow down as the actual frame rate fluctuates under different device/load conditions.

THE FIX

Use the timestamp argument to calculate delta time between frames, and scale movement by that delta rather than assuming a fixed per-frame increment.

Real-World Examples

A Frame-Rate-Independent Progress Bar Animation

A loading progress bar needed to animate smoothly to a target percentage over a fixed real-world duration, regardless of the device's actual achieved frame rate.

function animateProgress(target, durationMs) {
  const start = performance.now();
  function step(now) {
    const elapsed = now - start;
    const progress = Math.min(elapsed / durationMs, 1);
    bar.style.width = `${progress * target}%`;
    if (progress < 1) requestAnimationFrame(step);
  }
  requestAnimationFrame(step);
}

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

An animation loop that keeps running after its element is removed

cancelAnimationFrame(frameId);

The Solution //

Store the frame id and call cancelAnimationFrame() during cleanup.

Lesson Glossary

[01]requestAnimationFrame

Schedules a callback to run right before the browser's next repaint.

Code Preview
requestAnimationFrame(fn)

[02]cancelAnimationFrame

Cancels a previously scheduled requestAnimationFrame callback.

Code Preview
cancelAnimationFrame(id)

[03]Delta Time

The elapsed time between two animation frames, used for frame-rate-independent motion.

Code Preview
timestamp - lastTime

[04]Repaint

The browser's process of redrawing pixels to the screen, which requestAnimationFrame times its callback around.

Code Preview
browser repaint cycle

[05]Frame-Rate Independence

Animation logic that produces consistent perceived speed regardless of the actual achieved frame rate.

Code Preview
speed * delta

Continue Learning