I am using the setInterval() function to call a function every 20 seconds. However, one thing I noticed is that it the first time setInterval() actually calls the function is at 20 seconds (and not when setInterval() is called). Here is my current workaround:
dothis();
var i = setInterval(dothis, 20000);
Is there any way to achieve this without having this duplicate code?
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:
Then, in your code, you could just do this: