Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Animation.cancel(): canceling an animation

Animation.cancel() aborts an animation and removes its effect on the element rather than holding the current frame.

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

animation.cancel();

animation.cancel() is a Web Animations method, not part of ECMAScript itself. It takes no arguments and returns undefined. It sets playState to idle and both currentTime and startTime to null.

It removes the animation’s effect, including with fill: 'both'; it does not save a frame into styles. Unlike pause(), it does not preserve the current position; unlike finish(), it does not seek to the end. The object can be restarted with play().

For a non-idle animation, it replaces the existing finished promise and queues a cancel event. The old promise is rejected with AbortError only if it is still pending. If it has already been fulfilled, it stays fulfilled. Attach rejection handling before canceling. The example requires a browser; Node 24 does not establish support for this API.

Examples

(async () => {
  const box = document.createElement('div');
  document.body.append(box);
  const animation = box.animate(
    [{ opacity: 0 }, { opacity: 1 }],
    { duration: 1000, fill: 'both' }
  );
  const outcome = animation.finished.catch(error => error.name);
  animation.cancel();
  console.log(animation.playState);
  console.log(animation.currentTime === null);
  console.log(animation.startTime === null);
  console.log(await outcome);
  box.remove();
})();

Result

idle
true
true
AbortError

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