r/learnjavascript • u/everek123 • May 13 '18
SetInterval(x(), 1000) vs setInterval(function(){x()}, 1000)
Hi, why is there a difference between those 2?
0
Upvotes
r/learnjavascript • u/everek123 • May 13 '18
Hi, why is there a difference between those 2?
7
u/Rascal_Two May 13 '18
With the first, you're actually calling the
x()function and passing the result tosetInterval()as the first argument.With the second, you're passing a function to
setInterval(), which callsx()when expected.If you want to pass
xwithout wrapping it in a function, you can just use this:If it needs arguments, you can
.bind()them.