@@ -315,12 +315,19 @@ function Animation( elem, properties, options ) {
315
315
316
316
deferred . notifyWith ( elem , [ animation , percent , remaining ] ) ;
317
317
318
+ // If there's more to do, yield
318
319
if ( percent < 1 && length ) {
319
320
return remaining ;
320
- } else {
321
- deferred . resolveWith ( elem , [ animation ] ) ;
322
- return false ;
323
321
}
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 ;
324
331
} ,
325
332
animation = deferred . promise ( {
326
333
elem : elem ,
@@ -385,6 +392,13 @@ function Animation( elem, properties, options ) {
385
392
animation . opts . start . call ( elem , animation ) ;
386
393
}
387
394
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
+
388
402
jQuery . fx . timer (
389
403
jQuery . extend ( tick , {
390
404
elem : elem ,
@@ -393,11 +407,7 @@ function Animation( elem, properties, options ) {
393
407
} )
394
408
) ;
395
409
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 ;
401
411
}
402
412
403
413
jQuery . Animation = jQuery . extend ( Animation , {
@@ -641,7 +651,7 @@ jQuery.fx.tick = function() {
641
651
for ( ; i < timers . length ; i ++ ) {
642
652
timer = timers [ i ] ;
643
653
644
- // Checks the timer has not already been removed
654
+ // Run the timer and safely remove it when done (allowing for external removal)
645
655
if ( ! timer ( ) && timers [ i ] === timer ) {
646
656
timers . splice ( i -- , 1 ) ;
647
657
}
@@ -654,11 +664,16 @@ jQuery.fx.tick = function() {
654
664
} ;
655
665
656
666
jQuery . fx . timer = function ( timer ) {
657
- jQuery . timers . push ( timer ) ;
667
+ var i = jQuery . timers . push ( timer ) - 1 ,
668
+ timers = jQuery . timers ;
669
+
658
670
if ( timer ( ) ) {
659
671
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 ) ;
662
677
}
663
678
} ;
664
679
0 commit comments