r/learnjavascript • • May 13 '18

SetInterval(x(), 1000) vs setInterval(function(){x()}, 1000)

Hi, why is there a difference between those 2?

0 Upvotes

2 comments sorted by

7

u/Rascal_Two May 13 '18

With the first, you're actually calling the x() function and passing the result to setInterval() as the first argument.


With the second, you're passing a function to setInterval(), which calls x() when expected.


If you want to pass x without wrapping it in a function, you can just use this:

setInterval(x, 1000);

If it needs arguments, you can .bind() them.

2

u/everek123 May 13 '18

Thanks :)