Call an anonymous function defined in a setInterval

Posted by Tominator on Stack Overflow See other posts from Stack Overflow or by Tominator
Published on 2010-06-03T08:02:12Z Indexed on 2010/06/03 8:04 UTC
Read the original article Hit count: 181

Filed under:

Hi,

I've made this code:

window.setInterval(function(){ var a = doStuff(); var b = a + 5; }, 60000)

The actual contents of the anonymous function is of course just for this small example as it doesn't matter. What really happens is a bunch of variables get created in the scope of the function itself, because I don't need/want to pollute the global space.

But as you all know, the doStuff() function won't be called until 60 seconds in the page. I would also like to call the function right now, as soon as the page is loaded, and from then on every 60 seconds too.

Is it somehow possible to call the function without copy/pasting the inside code to right after the setInterval() line? As I said, I don't want to pollute the global space with useless variables that aren't needed outside the function.

© Stack Overflow or respective owner

Related posts about JavaScript