liveBook · Manning

requestAnimationFrame

As explained in Web Components in Action

requestAnimationFrame is a browser API that schedules a callback to run before the next screen repaint.

requestAnimationFrame schedules a callback for the browser’s render cycle. Unlike setInterval, each callback runs once, so the update method must request the next frame itself. section 4.5

A self-rescheduling visual countdown

Initialize a timer at 200, update the element’s width on each callback, reset it when it reaches zero, and schedule update again with requestAnimationFrame. The result is a green component that shrinks each frame and then restarts. section 4.5

Listing 4.9. Swapping setInterval for requestAnimationFrame
<html>
<head>
    <title>Visual Countdown Timer</title>
    <script>
        class VisualCountdownTimer extends HTMLElement {
            connectedCallback() {
                this.timer = 200;
                this.style.backgroundColor = 'green';
                this.style.display = 'inline-block';
                this.style.height = '50px';
                requestAnimationFrame( () =>        #1
                       this.update());
            }

            update() {
                this.timer --;
                if (this.timer <= 0) {
                    this.timer = 200;
                }
                this.style.width =                  #2
                       this.timer + 'px';
                requestAnimationFrame( () =>        #3
                       this.update());
        }

        customElements.define('countdown-timer', VisualCountdownTimer);
    </script>
</head>
<body>
    <countdown-timer></countdown-timer>
</body>
</html>

The listing shows the complete swap from setInterval to a recurring requestAnimationFrame update loop. section 4.5

How Web Components in Action covers it

How deep it goes
Explains one-shot callbacks, explicit rescheduling, visual updates, and an inheritable per-frame component pattern.
Examples
Uses a green visual countdown timer that shrinks each frame and resets after reaching zero.
What it assumes
Uses basic JavaScript timers, DOM styling, custom elements, and HTMLElement inheritance.
What you can do after
Apply it via a recurring callback loop to animate a custom element on each browser frame.
How it connects
Connects browser animation loops with setInterval, custom elements, HTMLElement inheritance, and game engine lifecycle concepts.

Also explained in

Compare how 2 books explain requestAnimationFrame →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.