当计时器为 运行 in d3.js 时是否可以改变时间间隔?

Is it possible to vary the time interval while the timer is running in d3.js?

我希望能够调整回调之间的时间间隔。我按以下方式使用 d3 的 interval

let timeInterval = 500;
d3.interval(callback, timeInterval);

现在,在执行期间的某个时刻,我希望能够调整 timeInterval 的值(通过用户输入)并让它反映在后续回调的执行速度中。 这可能吗?如果可能,怎么做?

d3.interval() 显然 returns 创建的计时器对象。

您可以stop the old timer并开始一个新的:

var intervalTimer;
// ... other code ...
intervalTimer = d3.interval(callback, 500);
// ... other code ...
intervalTimer.stop();
intervalTimer = d3.interval(callback, 100);
// ...

更改间隔行为(包括其计时)的最简单方法是调用定期执行的 .restart() method on it. Since a d3.interval() is just a wrapped d3.timer(),它还具有上述 .restart() 方法。

# 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 invocation priority.

这可以通过以下方式完成:

const callback = console.log;
const interval = d3.interval(callback, 500);  // initial timing 500ms
interval.restart(callback, 1000);             // updated timing 1000ms
<script src="https://d3js.org/d3.v6.js"></script>