JSPM

  • Created
  • Published
  • Downloads 15860441
  • Score
    100M100P100Q220160F
  • License BSD-3-Clause

An efficient queue capable of managing thousands of concurrent animations.

Package Exports

  • d3-timer

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (d3-timer) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

d3-timer

This module provides an efficient queue capable of managing thousands of concurrent animations, while guaranteeing consistent, synchronized timing with concurrent or staged animations. Internally, it uses requestAnimationFrame for fluid animation (if available), switching to setTimeout for delays longer than 24ms.

Installing

If you use NPM, npm install d3-timer. Otherwise, download the latest release. The released bundle supports AMD, CommonJS, and vanilla environments. Create a custom build using Rollup or your preferred bundler. You can also load directly from d3js.org:

<script src="https://d3js.org/d3-timer.v0.1.min.js"></script>

In a vanilla environment, a d3_timer global is exported. Try d3-timer in your browser.

API Reference

# d3.timer(callback[, delay[, time]])

Schedules a new timer, invoking the specified callback repeatedly until the timer is stopped. An optional numeric delay in milliseconds may be specified to invoke the given callback after a delay; if delay is not specified, it defaults to zero. The delay is relative to the specified time in milliseconds since UNIX epoch; if time is not specified, it defaults to Date.now.

The callback is passed two arguments when it is invoked: the elapsed time since the timer became active, and the current time, now. The latter is useful for precise scheduling of secondary timers. For example:

var t = d3.timer(function(elapsed, now) {
  console.log(elapsed, now);
  if (elapsed > 200) t.stop();
}, 150);

This produced the following console output:

6 1433806724202
26 1433806724222
51 1433806724247
73 1433806724269
92 1433806724288
114 1433806724310
132 1433806724328
152 1433806724348
171 1433806724367
191 1433806724387
213 1433806724409

Note that the first elapsed is 6ms, since this is the elapsed time since the timer started (after the 150ms delay), not the elapsed time since the timer was scheduled.

Use delay and time to specify relative and absolute moments in time when the callback should start being invoked. For example, a calendar notification for four hours before midnight on October 29, 2012 might be coded as:

d3.timer(callback, -4 * 1000 * 60 * 60, new Date(2012, 09, 29));

If timer is called within the callback of another timer, the new timer callback (if eligible as determined by the specified delay and time) will be invoked immediately at the end of the current frame, rather than waiting until the next frame. Within a frame, timer callbacks are guaranteed to be invoked in the order they were scheduled (regardless of their start time).

# timer.restart(callback[, delay[, time]])

Restart a timer with the specified callback and optional delay and time. This is equivalent to stopping this timer and creating a new timer with the specified arguments, although this timer retains the original id and invocation priority.

# timer.stop()

Stops this timer, preventing subsequent callbacks. This method has no effect if the timer has already stopped.

# timer.id

An opaque, unique identifier for this timer.

# d3.timerOnce(callback[, delay[, time]])

Like timer, except the timer automatically stops on its first callback.

# d3.timerFlush([time])

Immediately execute (invoke once) any eligible timer callbacks. If time is specified, it represents the current time; if not specified, it defaults to Date.now. Specifying an explicit time helps ensure deterministic behavior.

Note that zero-delay timers are normally first executed after one frame (~17ms). This can cause a brief flicker because the browser renders the page twice: once at the end of the first event loop, then again immediately on the first timer callback. By flushing the timer queue at the end of the first event loop, you can run any zero-delay timers immediately and avoid the flicker.