Skip to content

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

Closed
@chenjsh36

Description

@chenjsh36

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!

Activity

mbostock

mbostock commented on Jul 19, 2016

@mbostock
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mbostock@chenjsh36

        Issue actions

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