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?
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With