Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear the interval in d3.js

Tags:

d3.js

I'm using d3.interval instead of the javascript setInterval. The problem is that I don't know how to stop it, since I was using ClearInterval and it clearly doesn't work.

Thanks.

like image 765
Nuno Lopes Avatar asked Oct 11 '17 14:10

Nuno Lopes


People also ask

How do you clear setInterval?

Answer: Use the clearInterval() Method The setInterval() method returns an interval ID which uniquely identifies the interval. You can pass this interval ID to the global clearInterval() method to cancel or stop setInterval() call.

What is an interval in d3?

The d3. interval() function is used to call the function after every given time interval or delay. If the delay is not given then the delay is equal to the timer. Syntax: d3.

How do you know if an interval is cleared?

When you call clearInterval, it gets the interval associated with that id and clears it. The id isn't changed at all. This will show you the interval's id (returned by setInterval earlier). If you know the interval's id is 1, you can just use clearInterval(1) to clear the interval.

How do I clear an interval in JavaScript?

To clear an interval in JavaScript, use the clearInterval () function and pass the interval object as the first argument. The example below demonstrates running a function in an interval using the setInterval () then stopping it. The above isn't a practical example as the interval is stopped before it even completes the first iteration.

What does clearinterval () do in JavaScript?

The clearInterval () method in JavaScript clears the timer which has been set by a setInterval () method. In the code above, the setInterval () method will run the function every 1000 milliseconds (1 second). We can change that parameter to be however often we want to execute the function call.

How to clear all values from a set in D3?

The set.clear () function in D3.js is used to remove all values from the set. It clears the set and holds that blank set you can check that in the 2nd example. Parameters: This function does not accept any parameters.

How do I clear an interval from a timer set?

More examples below. The clearInterval () method clears a timer set with the setInterval () method. To clear an interval, use the id returned from setInterval (): Required. The interval id returned from setInterval ().


1 Answers

Use method stop for this. This method works for both d3.timer and d3.interval;

var interval = d3.interval(function(elapsed) {
  if (elapsed > 2600) {
    interval.stop(); // <== !!!
    return;
  }
  
  console.log(elapsed);
}, 500);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.11.0/d3.min.js"></script>
like image 144
Mikhail Shabrikov Avatar answered Nov 15 '22 11:11

Mikhail Shabrikov