TExcelTAD
Home › jQuery › jQuery stop()

jQuery stop()

Animations take time. If a user triggers an effect again before the previous one has finished — hovering in and out quickly, clicking a button five times — jQuery queues every request, and the element keeps animating long after the user has stopped. .stop() solves this by halting the current animation.

Syntax

js
$(selector).stop(clearQueue, jumpToEnd);
Call Effect
.stop() Stops the current animation where it is; the next queued animation starts
.stop(true) Stops the current animation and clears the queue
.stop(true, true) Clears the queue and jumps the current animation to its end state
.stop(false, true) Jumps the current one to its end, then continues with the queue
.finish() Stops everything and puts all queued animations in their end state

Seeing the difference

Start a long animation, then press one of the stop buttons while it is still moving.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; font-size: 14px; }
    #track { position: relative; height: 70px; background: #f4f6f8; margin-top: 8px; }
    #car { position: absolute; left: 0; top: 15px; width: 60px; height: 40px; background: #c0392b; border-radius: 6px; }
  </style>
</head>
<body>
  <button id="start">Start (3 legs)</button>
  <button id="s1">stop()</button>
  <button id="s2">stop(true)</button>
  <button id="s3">stop(true, true)</button>
  <button id="s4">finish()</button>
  <div id="track"><div id="car"></div></div>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    const $car = $("#car");
    function report(label) {
      console.log(label, "→ left:", Math.round(parseFloat($car.css("left"))),
                  "| queued:", $car.queue("fx").length);
    }
    $("#start").on("click", function () {
      $car.stop(true).css("left", 0)
        .animate({ left: 120 }, 1500)
        .animate({ left: 240 }, 1500)
        .animate({ left: 0 }, 1500);
    });
    $("#s1").on("click", () => { $car.stop(); report("stop()"); });
    $("#s2").on("click", () => { $car.stop(true); report("stop(true)"); });
    $("#s3").on("click", () => { $car.stop(true, true); report("stop(true, true)"); });
    $("#s4").on("click", () => { $car.finish(); report("finish()"); });
  </script>
</body>
</html>

What you should observe:

  • stop() — the current leg is abandoned and the next leg starts at once, from wherever the car was.
  • stop(true) — the car freezes exactly where it is.
  • stop(true, true) — the car jumps to the end of the current leg and stays there.
  • finish() — the car jumps to the end of the last leg (back at left: 0).

Fixing hover "queue build-up"

The most common real-world use: hover effects. Move the mouse quickly back and forth over both boxes. The left one (without stop) keeps bouncing long after you stop; the right one reacts instantly.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; }
    .col { display: inline-block; width: 150px; vertical-align: top; margin-right: 12px; }
    .btn { padding: 10px; background: #0769ad; color: #fff; text-align: center; cursor: pointer; }
    .tip { display: none; padding: 8px; background: #eef5fb; font-size: 13px; }
  </style>
</head>
<body>
  <div class="col" id="bad">
    <div class="btn">Without stop()</div>
    <div class="tip">I keep sliding up and down after you leave…</div>
  </div>
  <div class="col" id="good">
    <div class="btn">With stop()</div>
    <div class="tip">I always follow the mouse.</div>
  </div>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    $("#bad").on("mouseenter", function () {
      $(this).find(".tip").slideDown(400);
    }).on("mouseleave", function () {
      $(this).find(".tip").slideUp(400);
    });

    $("#good").on("mouseenter", function () {
      $(this).find(".tip").stop(true).slideDown(400);
    }).on("mouseleave", function () {
      $(this).find(".tip").stop(true).slideUp(400);
    });
  </script>
</body>
</html>

.stop(true) clears the queue before starting the new slide, so only the latest intent ("open" or "close") is ever animated, and the new slide continues from the current height.

Other ways to manage the queue

Method What it does
.queue("fx") Returns the array of queued animation functions
.clearQueue() Removes queued items that have not started yet
.delay(ms) Inserts a pause into the queue
.is(":animated") true while an element is animating
html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; }
    #msg { display: none; padding: 10px; background: #2e7d32; color: #fff; width: 220px; border-radius: 6px; }
  </style>
</head>
<body>
  <button id="ping">Notify</button>
  <p><small>Click repeatedly: the message is restarted instead of queued.</small></p>
  <div id="msg">Saved!</div>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    let n = 0;
    $("#ping").on("click", function () {
      n++;
      console.log("animating before click?", $("#msg").is(":animated"));
      $("#msg")
        .stop(true, true)        // finish whatever was running, drop the rest
        .text("Saved! (" + n + ")")
        .fadeIn(200)
        .delay(1200)
        .fadeOut(400);
    });
  </script>
</body>
</html>

Note: .delay() only delays items in the animation queue. It does not delay non-animation methods such as .text() or .css(), which run immediately.

Test yourself

What does .stop(true) do?

  1. Stops only the current animation; queued ones continue
  2. Stops the current animation and clears the queue
  3. Pauses all animations on the page
  4. Jumps to the end of all animations

Which method immediately puts every queued animation of an element in its final state?

  1. .stop()
  2. .stop(true)
  3. .finish()
  4. .clearQueue()

Why is .stop(true) commonly added before hover animations?

  1. To make them faster
  2. To prevent queued animations from building up when the mouse moves quickly
  3. Because slideDown requires it
  4. To disable the animation