Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var elemdisplay = {},
// opacity animations
[ "opacity" ]
],
fxNow,
fxNow, ticks = 0,
requestAnimationFrame = window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame;
Expand Down Expand Up @@ -421,7 +421,7 @@ jQuery.fx.prototype = {
};
requestAnimationFrame( raf );
} else {
timerId = setInterval( fx.tick, fx.interval );
timerId = setInterval( fx.tick, 16 );
}
}
},
Expand Down Expand Up @@ -521,18 +521,20 @@ jQuery.fx.prototype = {

jQuery.extend( jQuery.fx, {
tick: function() {
for ( var timers = jQuery.timers, i = 0 ; i < timers.length ; ++i ) {
if ( !timers[i]() ) {
timers.splice(i--, 1);
}
}
if ( this.throttle == 1 || ++ticks >= this.throttle && !( ticks = 0 ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a === in the throttle check and ticks always resets to 0 here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ticks should only reset to 0 when the previous test is true, isn't it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I wasn't sure that's what you wanted though. ticks never seems to get higher than 1, but maybe that's ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will, if developers choose to throttle the animation mechanism
jQuery.fx.throttle = 2 // animation will run at a maximum of 30fps

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, forgive my confusion. Need to sleep more. ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still, let's work out some test cases for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do it asap.

for ( var timers = jQuery.timers, i = 0 ; i < timers.length ; ++i ) {
if ( !timers[i]() ) {
timers.splice(i--, 1);
}

if ( !timers.length ) {
jQuery.fx.stop();
if ( !timers.length ) {
jQuery.fx.stop();
}
}
}
},

interval: 13,
throttle: 1,

stop: function() {
clearInterval( timerId );
Expand Down
24 changes: 24 additions & 0 deletions test/unit/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -1045,3 +1045,27 @@ test("callbacks should fire in correct order (#9100)", function() {
}
});
});

test("throttle animation ticks (#9384)", function() {
stop();
var ticks = 0,
obj = { test: 0 };

jQuery.fx.throttle = 2;
jQuery(obj).animate({test: 100}, {
step: function() {
ticks++;
},
duration: 1000,
complete: function() {
ok( ticks > 0, "When throttle value is != 1, animation still runs" );
// When throttle value is 1 (no throttling), there should be 60 ticks per second.
// A throttle value of 2 means 60fps/2, 3 is 60fps/3, ...
// The exact number of ticks per second isn't predictable.
// Here we just make sure that there will be less than 35 ticks in one second.
ok( ticks < 35, "When throttle value is 2, animation is running at ~30fps" );

start();
}
});
});