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.
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.
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.
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.
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.
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.
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.
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 ().
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>
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