Skip to content

Commit

Permalink
Added a new .stop() method which stops all animations running on the …
Browse files Browse the repository at this point in the history
…matched set of elements.

Example:
  $("#foo").slideDown(1000);
  setTimeout(function(){
    $("#foo").stop();
  }, 500);
  • Loading branch information
jeresig committed Sep 4, 2007
1 parent d5bb0e3 commit 1ce8006
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/fx/fx.js
Expand Up @@ -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);
});
}

});
Expand Down Expand Up @@ -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(){
Expand Down
23 changes: 23 additions & 0 deletions src/fx/fxTest.js
Expand Up @@ -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");
Expand Down

0 comments on commit 1ce8006

Please sign in to comment.