Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

cancelAnimationFrame — canceling a pending callback

cancelAnimationFrame(handle) removes a pending callback registered with requestAnimationFrame(). Use the returned identifier in the same context; the method does not interrupt code that is already running.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

window.cancelAnimationFrame(handle);

cancelAnimationFrame(handle) cancels one pending callback registered with requestAnimationFrame(). Pass the returned identifier handle, not the callback function, and use the same API provider object. The method returns undefined.

An identifier no longer present among the pending callbacks changes nothing. Cancellation neither interrupts a callback that has started nor reverses its effects. In a loop that schedules subsequent frames, save the latest identifier and cancel that one; an earlier identifier does not represent the entire animation.

This is a Web API from the HTML Living Standard, not an ECMAScript function. It is supported by current browser engines for Window. The standard also provides it in supported DedicatedWorkerGlobalScope contexts; an unsupported context throws NotSupportedError. The Node version used in a repository does not establish this API's availability.

The example cancels the first callback, then checks the counter in a second callback. The expected output is calls: 0 and result: undefined. That check happens only when the browser runs the second callback.

Examples

(() => {
  let calls = 0;
  const handle = window.requestAnimationFrame(() => {
    calls += 1;
  });

  const result = window.cancelAnimationFrame(handle);

  window.requestAnimationFrame(() => {
    console.log(`calls: ${calls}`);
    console.log(`result: ${result}`);
  });
})();

Result

calls: 0
result: undefined

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources