@@ -23,6 +23,8 @@ var MainLoop = function (framerate)
2323 */
2424 this . frameDelta = 0 ;
2525
26+ this . discardedTime = 0 ;
27+
2628 /**
2729 * The timestamp in milliseconds of the last time the main loop was run.
2830 * Used to compute the time elapsed between frames.
@@ -134,9 +136,11 @@ MainLoop.prototype = {
134136
135137 step : function ( timestamp , active , renderer )
136138 {
139+ var len = active . length ;
140+
137141 // Throttle the frame rate (if minFrameDelay is set to a non-zero value by
138142 // `MainLoop.setMaxAllowedFPS()`).
139- if ( active . length === 0 || timestamp < this . lastFrameTimeMs + this . minFrameDelay )
143+ if ( len === 0 || timestamp < this . lastFrameTimeMs + this . minFrameDelay )
140144 {
141145 return ;
142146 }
@@ -152,7 +156,7 @@ MainLoop.prototype = {
152156 // Run any updates that are not dependent on time in the simulation.
153157 // Here we'll need to run things like tween.update, input.update, etc.
154158
155- for ( var i = 0 ; i < active . length ; i ++ )
159+ for ( var i = 0 ; i < len ; i ++ )
156160 {
157161 active [ i ] . state . sys . begin ( timestamp , this . frameDelta ) ;
158162 }
@@ -178,7 +182,7 @@ MainLoop.prototype = {
178182
179183 while ( this . frameDelta >= this . timestep )
180184 {
181- for ( var i = 0 ; i < active . length ; i ++ )
185+ for ( i = 0 ; i < len ; i ++ )
182186 {
183187 active [ i ] . state . sys . update ( this . timestep , this . physicsStep ) ;
184188 }
@@ -198,6 +202,7 @@ MainLoop.prototype = {
198202
199203 renderer . preRender ( ) ;
200204
205+ // This uses active.length, in case state.update removed the state from the active list
201206 for ( i = 0 ; i < active . length ; i ++ )
202207 {
203208 active [ i ] . state . sys . render ( interpolation , renderer ) ;
@@ -211,9 +216,9 @@ MainLoop.prototype = {
211216 // it's better than the alternative (the application would look like it
212217 // was running very quickly until the simulation caught up to real
213218 // time).
214- var discardedTime = Math . round ( this . resetFrameDelta ( ) ) ;
219+ this . discardedTime = Math . round ( this . resetFrameDelta ( ) ) ;
215220
216- console . warn ( 'Main loop panicked, tab probably put in the background. Discarding ' + discardedTime + 'ms' ) ;
221+ // console.warn('Main loop panicked, tab probably put in the background. Discarding ' + discardedTime + 'ms');
217222 }
218223
219224 this . panic = false ;
0 commit comments