@@ -315,12 +315,19 @@ function Animation( elem, properties, options ) {
315315
316316 deferred . notifyWith ( elem , [ animation , percent , remaining ] ) ;
317317
318+ // If there's more to do, yield
318319 if ( percent < 1 && length ) {
319320 return remaining ;
320- } else {
321- deferred . resolveWith ( elem , [ animation ] ) ;
322- return false ;
323321 }
322+
323+ // If this was an empty animation, synthesize a final progress notification
324+ if ( ! length ) {
325+ deferred . notifyWith ( elem , [ animation , 1 , 0 ] ) ;
326+ }
327+
328+ // Resolve the animation and report its conclusion
329+ deferred . resolveWith ( elem , [ animation ] ) ;
330+ return false ;
324331 } ,
325332 animation = deferred . promise ( {
326333 elem : elem ,
@@ -385,6 +392,13 @@ function Animation( elem, properties, options ) {
385392 animation . opts . start . call ( elem , animation ) ;
386393 }
387394
395+ // Attach callbacks from options
396+ animation
397+ . progress ( animation . opts . progress )
398+ . done ( animation . opts . done , animation . opts . complete )
399+ . fail ( animation . opts . fail )
400+ . always ( animation . opts . always ) ;
401+
388402 jQuery . fx . timer (
389403 jQuery . extend ( tick , {
390404 elem : elem ,
@@ -393,11 +407,7 @@ function Animation( elem, properties, options ) {
393407 } )
394408 ) ;
395409
396- // attach callbacks from options
397- return animation . progress ( animation . opts . progress )
398- . done ( animation . opts . done , animation . opts . complete )
399- . fail ( animation . opts . fail )
400- . always ( animation . opts . always ) ;
410+ return animation ;
401411}
402412
403413jQuery . Animation = jQuery . extend ( Animation , {
@@ -641,7 +651,7 @@ jQuery.fx.tick = function() {
641651 for ( ; i < timers . length ; i ++ ) {
642652 timer = timers [ i ] ;
643653
644- // Checks the timer has not already been removed
654+ // Run the timer and safely remove it when done (allowing for external removal)
645655 if ( ! timer ( ) && timers [ i ] === timer ) {
646656 timers . splice ( i -- , 1 ) ;
647657 }
@@ -654,11 +664,16 @@ jQuery.fx.tick = function() {
654664} ;
655665
656666jQuery . fx . timer = function ( timer ) {
657- jQuery . timers . push ( timer ) ;
667+ var i = jQuery . timers . push ( timer ) - 1 ,
668+ timers = jQuery . timers ;
669+
658670 if ( timer ( ) ) {
659671 jQuery . fx . start ( ) ;
660- } else {
661- jQuery . timers . pop ( ) ;
672+
673+ // If the timer finished immediately, safely remove it (allowing for external removal)
674+ // Use a superfluous post-decrement for better compressibility w.r.t. jQuery.fx.tick above
675+ } else if ( timers [ i ] === timer ) {
676+ timers . splice ( i -- , 1 ) ;
662677 }
663678} ;
664679
0 commit comments