animation
JavaScript
cancel
web development
coding tips

How to stop an animation cancel does not work

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

If cancel() does not appear to stop an animation, the first thing to check is what kind of animation you are actually controlling. cancel() is part of the Web Animations API and only affects the Animation object it was called on. If the motion is driven by CSS classes, transitions, or a different JavaScript loop, calling cancel() on the wrong thing will not change what is on screen.

Make Sure You Have a Real Animation Object

With the Web Animations API, element.animate(...) returns an Animation instance. That is the object whose playback you can stop.

javascript
1const box = document.querySelector('.box');
2
3const animation = box.animate(
4  [
5    { transform: 'translateX(0px)' },
6    { transform: 'translateX(300px)' }
7  ],
8  {
9    duration: 2000,
10    fill: 'forwards'
11  }
12);
13
14setTimeout(() => {
15  animation.cancel();
16}, 500);

If this animation is the only source of motion, the box snaps back to its pre-animation state because cancel() removes the effect.

Why cancel() Can Seem Not to Work

The most common reasons are:

  • a CSS transition or class change is still applying the same visual state
  • another animation starts immediately afterward
  • you expected the element to stay where it was, but cancel() resets the effect
  • the reference you are calling is not the active animation object

That last point matters a lot in UI code with repeated event handlers. If the element starts a new animation each time, old references may no longer control the visible one.

Use pause() When You Want to Freeze in Place

If the goal is to stop movement without resetting the element to its original style, pause() is often the better method.

javascript
1const animation = box.animate(
2  [{ opacity: 1 }, { opacity: 0.2 }],
3  { duration: 3000, fill: 'forwards' }
4);
5
6setTimeout(() => {
7  animation.pause();
8}, 800);

pause() freezes the current progress. cancel() removes the animation effect. Those are different semantics, and mixing them up is a common source of confusion.

Preserve the Current Style Before Canceling

Sometimes you want to stop the animation and keep the current visual state. A practical pattern is to snapshot the animated styles first and then cancel.

javascript
1const animation = box.animate(
2  [{ transform: 'scale(1)' }, { transform: 'scale(1.4)' }],
3  { duration: 1500, fill: 'forwards' }
4);
5
6setTimeout(() => {
7  animation.commitStyles();
8  animation.cancel();
9}, 700);

commitStyles() writes the current computed animation state into inline styles, so canceling afterward does not visually revert the element.

CSS Animations Need a Different Stop Mechanism

If the animation is driven by CSS classes rather than element.animate, cancel() is not the correct tool. Instead, remove the class or change the property that triggered the CSS animation or transition.

javascript
box.classList.remove('moving');

That is why identifying the animation source is the first debugging step.

Common Pitfalls

  • Calling cancel() on an old Animation reference while a newer animation is visible.
  • Expecting cancel() to freeze the current frame instead of resetting the effect.
  • Using cancel() for CSS transitions that are not controlled by the Web Animations API.
  • Forgetting that another class or animation may still be affecting the same property.
  • Reaching for animation control methods before confirming which API created the animation.

Summary

  • 'cancel() only affects a real Web Animations API Animation object.'
  • It stops the animation and removes its effect, which often means reverting to the original state.
  • Use pause() when you want to freeze the current progress.
  • Use commitStyles() before canceling if you want to preserve the current visual state.
  • If the motion comes from CSS rather than WAAPI, stop the CSS trigger instead of calling cancel().

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.