Added a new .stop() method which stops all animations running on the matched set...
authorJohn Resig <jeresig@gmail.com>
Tue, 4 Sep 2007 00:28:22 +0000 (00:28 +0000)
committerJohn Resig <jeresig@gmail.com>
Tue, 4 Sep 2007 00:28:22 +0000 (00:28 +0000)
Example:
  $("#foo").slideDown(1000);
  setTimeout(function(){
    $("#foo").stop();
  }, 500);

src/fx/fx.js
src/fx/fxTest.js

index 075d1ab..69885e8 100644 (file)
@@ -368,6 +368,16 @@ jQuery.fn.extend({
                        if ( this.queue[type].length == 1 )
                                fn.apply(this);
                });
+       },
+
+       stop: function(){
+               var timers = jQuery.timers;
+
+               return this.each(function(){
+                       for ( var i = 0; i < timers.length; i++ )
+                               if ( timers[i].elem == this )
+                                       timers.splice(i--, 1);
+               });
        }
 
 });
@@ -466,9 +476,13 @@ jQuery.extend({
                        z.now = from;
                        z.a();
 
-                       jQuery.timers.push(function(){
+                       function t(){
                                return z.step(from, to);
-                       });
+                       }
+
+                       t.elem = elem;
+
+                       jQuery.timers.push(t);
 
                        if ( jQuery.timers.length == 1 ) {
                                var timer = setInterval(function(){
index 3c0d57d..1f9e674 100644 (file)
@@ -11,6 +11,29 @@ test("animate(Hash, Object, Function)", function() {
        });
 });
 
+test("stop()", function() {
+       expect(3);
+       stop();
+       reset();
+
+       var foo = $("#foo")[0];
+       var h = foo.style.height;
+
+       $("#foo").slideUp(1000);
+       setTimeout(function(){
+               var nh = foo.style.height;
+               ok( nh != h, "An animation occurred " + nh + " " + h );
+               $("#foo").stop();
+
+               nh = foo.style.height;
+               ok( nh != h, "Stop didn't reset the animation " + nh + " " + h );
+               setTimeout(function(){
+                       equals( nh, foo.style.height, "The animation didn't continue" );
+                       start();
+               }, 100);
+       }, 100);
+});
+
 test("toggle()", function() {
        expect(3);
        var x = $("#foo");