Browser rendering opportunities
How it works
Browser rendering is coordinated with the HTML event loop. requestAnimationFrame schedules a callback for an animation frame before the associated repaint opportunity; it is not a guarantee of a frame every fixed number of milliseconds. Long synchronous tasks and microtask chains can delay rendering and input. Use frame callbacks for small visual updates, compute heavier work separately and avoid mixing repeated layout reads with writes.
Example 1
Save each file separately. Serve with a local HTTP server and open index.html. This is a multi-file example, not a single editor snippet.
index.html
<!doctype html><meta charset="utf-8"><title>Frame</title><p id="out">start</p><script> requestAnimationFrame(()=>{document.getElementById("out").textContent="updated";console.log("frame");});console.log("script");</script>Expected output in the tested environment:
scriptframeThe frame callback updates the DOM after the synchronous script. The test checks the callback and DOM, not pixels or frame rate.
Common mistake
Do not use requestAnimationFrame as a precise timer or assume it continues normally in hidden tabs.
Practice
Add a second frame callback and cancel it using cancelAnimationFrame before it runs. Inspect the DOM after the first callback.
Run this practice in the documented local environment. ByteMind module and asynchronous grader support is not yet verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Browser rendering opportunities — Knowledge Check
Reason about the concept and predict its behavior in the indicated environment.
Which host API schedules a callback for an upcoming animation frame?
Does a frame callback prove that the browser displayed pixels immediately afterward?
What can delay input and rendering on the browser main thread?
How should large visual work be organized?
For Browser rendering opportunities, what is the console output of example 1 in the browser fixture?
// File: index.html<!doctype html><meta charset="utf-8"><title>Frame</title><p id="out">start</p><script>requestAnimationFrame(()=>{document.getElementById("out").textContent="updated";console.log("frame");});console.log("script");</script>