Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d3.timer: Why set the d3_timer_step delay to 24? #2910

Closed
chenjsh36 opened this issue Jul 19, 2016 · 1 comment
Closed

d3.timer: Why set the d3_timer_step delay to 24? #2910

chenjsh36 opened this issue Jul 19, 2016 · 1 comment

Comments

@chenjsh36
Copy link

I am reading the code of D3.js of 3.0. The function d3_timer_step() confuse me:
function d3_timer_step() { var now = d3_timer_mark(), delay = d3_timer_sweep() - now; if (delay > 24) { if (isFinite(delay)) { clearTimeout(d3_timer_timeout); d3_timer_timeout = setTimeout(d3_timer_step, delay); } d3_timer_interval = 0; } else { d3_timer_interval = 1; d3_timer_frame(d3_timer_step); } }
If delay larger than 24ms, the step would call after delay ms, but why is 24? why not 16.7 or others ?
Could anyone tell me why? Thanks very much!

@mbostock
Copy link
Member

It’s arbitrary, and changing it by a few milliseconds would likely have no apparent effect. The accuracy of setTimeout tends to be fairly poor—because the browser and operating system can be doing other things, the callback can be off by 10-20 ms. Generally speaking using requestAnimationFrame is preferred for rendering tasks, since it tries to consolidate operations that modify the DOM before redrawing. 24 is about half-way between one frame (16.7) and two (33.3), so that’s when we switch from using requestAnimationFrame to setTimeout for the delay.

But again, this only matters if the shortest delay of any active timer is 24 ms. If there’s an active animation, for instance, then requestAnimationFrame tends to always be used, and slower timers may still be called back once they become active.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants