What is d3-timer?
The d3-timer package provides a set of tools for controlling the timing of animations, transitions, and other time-based operations. It allows for the efficient scheduling of new timers, the creation of timer queues, and the handling of elapsed time within scheduled callbacks.
What are d3-timer's main functionalities?
Creating and starting a timer
This feature allows you to create a new timer that calls a specified callback function on each animation frame with the elapsed time. The timer can be stopped by calling the stop method.
const timer = d3.timer(elapsed => {
console.log('Timer elapsed time:', elapsed);
if (elapsed > 2000) timer.stop();
});
Creating a timer with a delay and time limit
This feature allows you to create a timer with a delay before it starts invoking the callback function. The timer will automatically stop after a specified time limit.
const timer = d3.timer(elapsed => {
console.log('Timer elapsed time with delay:', elapsed);
if (elapsed > 1000) timer.stop();
}, 500);
Creating a timer queue
This feature allows you to create a queue of timers that will execute their callback functions after a specified delay. It is similar to using setTimeout but is integrated with the d3-timer's efficient timing mechanism.
const timer1 = d3.timeout(() => console.log('Timeout 1'), 1000);
const timer2 = d3.timeout(() => console.log('Timeout 2'), 2000);
Creating an interval timer
This feature allows you to create a timer that will execute its callback function at a specified interval. It is similar to using setInterval but provides more accurate and efficient timing.
const interval = d3.interval(() => {
console.log('Interval tick');
if (++count > 10) interval.stop();
}, 1000);
Other packages similar to d3-timer
raf-schd
raf-schd provides a scheduler for requestAnimationFrame, allowing you to throttle rendering updates for better performance. It is similar to d3-timer in that it helps manage animations, but it focuses on scheduling within the animation frame rate.
chrono-node
chrono-node is a high-resolution timer utility for Node.js, which can be used for performance testing and benchmarking. It offers precise timing but does not provide the same scheduling features as d3-timer.
later
later is a library for describing recurring schedules and calculating their occurrences. It provides a different set of features compared to d3-timer, focusing on recurring schedules rather than frame-based animation timing.
d3-timer
An efficient queue capable of managing thousands of concurrent animations. Also guarantees consistent, synchronized timing with concurrent or staged animations. Uses requestAnimationFrame for fluid animation, switching to setTimeout for delays longer than 24ms.
Installing
If you use NPM, npm install d3-timer
. Otherwise, download the latest release.
API Reference
# timer(callback[, delay[, time]])
Schedules a new timer, invoking the specified callback repeatedly until it returns true. (There is no API for canceling a timer; the callback must return a truthy value to terminate.) An optional numeric delay in milliseconds may be specified to invoke the given callback after a delay. 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 each time it is invoked: the elapsed time since the timer became active, and the current time. The latter is useful for precise scheduling of secondary timers. For example:
timer(function(elapsed, time) {
console.log(elapsed, time);
return elapsed > 200;
}, 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, 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 might be coded as:
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.
# 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 the time explicitly can 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.
# timerReplace(callback[, delay[, time]])
Replace the current timer’s callback, delay and time. This method can only be called within a timer callback, and is equivalent to timer, except that it replaces the current timer rather than scheduling a new timer.
Changes from D3 3.x:
-
Timer callbacks are now passed the current time as a second argument, in addition to the elapsed time; this is useful for precise scheduling of secondary timers.
-
A new timerReplace method has been added to replace the current timer within a timer callback.
-
The timer.flush method has been renamed timerFlush. This method now accepts an optional time argument representing the current time, and returns the time of the earliest next timer. Calling this method within a timer callback no longer causes a crash.
-
Calling timer within a timer callback no longer makes a duplicate requestAnimationFrame. Calling this method with a delay greater than 24ms when no earlier timers are active guarantees a setTimeout rather than a requestAnimationFrame.