Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery stop(true, true) to jump to end of all animations in queue

I have been using jQuery's stop(true, true) method to clear running animations so the next one starts immediately. I noticed that the first parameter, clearQueue, clears the entire animation queue but the second parameter, jumpToEnd, only jumps to the end of the currently running animation and not the ones that were removed from the queue. Is there a way to have it clear and jump to the end of all queued animations?

I've ended up chaining a few stop(false, true) calls instead, but this will only handle 3 queued animations, for example.

$(this)
  .stop(false, true)
  .stop(false, true)
  .stop(false, true)
  .addClass('hover', 200);

Edit:

I ended up adding my own method, stopAll, as per Ates Goral's answer:

$.fn.extend({ stopAll: function () {
    while (this.queue().length > 0)
      this.stop(false, true);
    return this;
  } });
$(this).stopAll().addClass('hover', 200);

I hope someone else finds this useful.

like image 900
Jesse Avatar asked Mar 17 '12 05:03

Jesse


2 Answers

Chaining multiple stop(false, true) makes sense. Instead of hard-coding a fixed number of chained calls, you could check the queue length:

while ($(this).queue().length) {
    $(this).stop(false, true);
}

Demo: http://jsfiddle.net/AB33X/

like image 106
Ates Goral Avatar answered Nov 16 '22 16:11

Ates Goral


jQuery 1.9 introduced the .finish() method, which achieves exactly that.

like image 4
Vsevolod Golovanov Avatar answered Nov 16 '22 16:11

Vsevolod Golovanov