Search code examples
d3.js

Custom d3interval creation


I want to create an interval in such a way so that the first callback happens immediately and subsequent callbacks happen at the specified del. Is it possible inside a single d3.interval?

console.log(new Date());
const del = 1000;

let ticker = d3.interval(e => {
    console.log(new Date(), 1);
    if (e > 3000)(ticker.stop())
}, del)
<script type="text/javascript" src="https://d3js.org/d3.v7.min.js"></script>


Solution

  • No, it isn't "possible in a single d3.interval". ds.interval doesn't have that capability.

    Instead, break out the anonymous function to a variable, start the interval, then call the function directly.

    console.log(new Date());
    const del = 1000;
    const intervalFunc = e => {
        console.log(new Date(), 1);
        if (e > 3000)(ticker.stop())
    };
    let ticker = d3.interval(intervalFunc, del)
    intervalFunc();