Cancelling an animation

suggest change

To cancel a scheduled requestAnimationFrame callback, pass cancelAnimationFrame the id returned by the last call. This example starts a loop and cancels the pending frame after one second. It does not resume. requestAnimationFrame is a browser API.

// stores the id returned from each call to requestAnimationFrame
let requestId;

// draw something
function draw(timestamp) {
    // do some animation
    // request next frame
    start();
}

// pauses the animation
function pause() {
    // pass in the id returned from the last call to requestAnimationFrame
    cancelAnimationFrame(requestId);
}

// begin the animation
function start() {
    // store the id returned from requestAnimationFrame
    requestId = requestAnimationFrame(draw);
}

// begin now
start();

// after a second, pause the animation
setTimeout(pause,1000);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents