Cancelling an animation
suggest changeTo 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);
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents