gpt4 book ai didi

javascript - 计时器在 d3.js 中运行时是否可以改变时间间隔?

转载 作者:行者123 更新时间:2023-12-04 08:38:48 24 4
gpt4 key购买 nike

我希望能够调整回调之间的时间间隔。我正在使用 d3 的 interval通过以下方式:

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

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

最佳答案

更改间隔行为(包括时间)的最简单方法是调用 .restart()方法就可以了。自d3.interval()只是一个包裹的 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>

关于javascript - 计时器在 d3.js 中运行时是否可以改变时间间隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64662104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com