Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set periodic timers in d3.js?

Tags:

d3.js

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);

then f seems to run once and then never again.

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?

like image 632
Dan Halperin Avatar asked Nov 15 '12 01:11

Dan Halperin


2 Answers

Looks like d3.interval is a thing, and is meant to be a replacement for setInterval: https://github.com/d3/d3-timer#interval

var interval = d3.timeout(callback, interval_time, optional_delay);
like image 81
Chris Tabor Avatar answered Sep 25 '22 06:09

Chris Tabor


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

setInterval(f, 5*60*1000);

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.

like image 26
nrabinowitz Avatar answered Sep 22 '22 06:09

nrabinowitz