JavaScript: How to get setInterval() to start now? [duplicate]

Your method is THE normal way way of doing it.

If this comes up over and over, you could make a utility function that would execute the handler first and then set the interval:

function setIntervalAndExecute(fn, t) {
    fn();
    return(setInterval(fn, t));
}

Then, in your code, you could just do this:

var i = setIntervalAndExecute(dothis, 20000);

Leave a Comment