Added a performance improvement to .hide()/.show() that helps to prevent constant...
[jquery.git] / src / fx.js
index 40bbc61..3d00515 100644 (file)
--- a/src/fx.js
+++ b/src/fx.js
@@ -1,4 +1,5 @@
 var elemdisplay = {},
+       timerId,
        fxAttrs = [
                // height animations
                [ "height", "marginTop", "marginBottom", "paddingTop", "paddingBottom" ],
@@ -43,7 +44,13 @@ jQuery.fn.extend({
                                                elemdisplay[ tagName ] = display;
                                        }
                                        
-                                       this[i].style.display = jQuery.data(this[i], "olddisplay", display);
+                                       jQuery.data(this[i], "olddisplay", display);
+                               }
+
+                               // Set the display of the elements in a second loop
+                               // to avoid the constant reflow
+                               for ( var i = 0, l = this.length; i < l; i++ ){
+                                       this[i].style.display = jQuery.data(this[i], "olddisplay");
                                }
                        }
                        
@@ -59,8 +66,14 @@ jQuery.fn.extend({
                                var old = jQuery.data(this[i], "olddisplay");
                                if ( !old && old !== "none" )
                                        jQuery.data(this[i], "olddisplay", jQuery.css(this[i], "display"));
+                       }
+
+                       // Set the display of the elements in a second loop
+                       // to avoid the constant reflow
+                       for ( var i = 0, l = this.length; i < l; i++ ){
                                this[i].style.display = "none";
                        }
+
                        return this;
                }
        },
@@ -221,7 +234,6 @@ jQuery.extend({
        },
 
        timers: [],
-       timerId: null,
 
        fx: function( elem, options, prop ){
                this.options = options;
@@ -273,10 +285,8 @@ jQuery.fx.prototype = {
 
                t.elem = this.elem;
 
-               jQuery.timers.push(t);
-
-               if ( t() && jQuery.timerId == null ) {
-                       jQuery.timerId = setInterval(function(){
+               if ( t() && jQuery.timers.push(t) == 1 ) {
+                       timerId = setInterval(function(){
                                var timers = jQuery.timers;
 
                                for ( var i = 0; i < timers.length; i++ )
@@ -284,8 +294,7 @@ jQuery.fx.prototype = {
                                                timers.splice(i--, 1);
 
                                if ( !timers.length ) {
-                                       clearInterval( jQuery.timerId );
-                                       jQuery.timerId = null;
+                                       clearInterval( timerId );
                                }
                        }, 13);
                }
@@ -351,11 +360,10 @@ jQuery.fx.prototype = {
                                if ( this.options.hide || this.options.show )
                                        for ( var p in this.options.curAnim )
                                                jQuery.attr(this.elem.style, p, this.options.orig[p]);
-                       }
-
-                       if ( done )
+                                       
                                // Execute the complete function
                                this.options.complete.call( this.elem );
+                       }
 
                        return false;
                } else {