如何在d3.js中设置周期性定时器? [英] How do you set periodic timers in d3.js?

查看:217
本文介绍了如何在d3.js中设置周期性定时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要每5分钟运行一个特定的功能。如果我写这样的代码:

I want to run a particular function every 5 minutes. If I write code like this:

function f() {
    console.log("hi");
    d3.timer(f, 5*60*1000);
    return true;
}

d3.timer(f, 5*60*1000);

然后 f

我通过创建 f 的克隆 f2实现了所需的行为 f 调用 d3.timer(f2) f2 调用 d3.timer(f)。这似乎是一个丑陋的黑客。有没有更好的方法?

I achieved the desired behavior by creating a clone of f called f2: f calls d3.timer(f2) and f2 call d3.timer(f). This seems like an ugly hack. Is there a better way?

推荐答案

这听起来像标准JavaScript的工作 setInterval / code>方法:

This sounds like a job for the standard JavaScript setInterval() method:

setInterval(f, 5*60*1000);

如果您需要在每次调用时运行动画,那么 d3 .timer 将是有用的 - 否则,标准 setInterval setTimeout 更容易。

If you need it to run an animation at each invocation, that's where d3.timer would be useful - otherwise, the standard setInterval and setTimeout methods are likely to be easier.

这篇关于如何在d3.js中设置周期性定时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆