If I call a named function using setTimeout() and setInterval() without the parentheses it works as expected. When I call the same function with the parentheses it either executes it immediately or gives an error.
I am looking for a deeper understanding in this matter then what I have found on the web. Would you guys please explain to me why this is true?
var func = function(){
console.log("Bowties are cool.");
}
setTimeout(func(), 1500);
// Prints "Bowties are cool." immediately
setInterval(func(), 1500);
// Throws an error
setInterval(func, 1500);
// Works as expected
setTimeout(console.log("Bowties are cool."),1500);
// This method has the same result as "setTimeout(func(), 1500)".
Methods setTimeout(func, delay, ... args) and setInterval(func, delay, ... args) allow us to run the func once/regularly after delay milliseconds. To cancel the execution, we should call clearTimeout/clearInterval with the value returned by setTimeout/setInterval .
Answer: Use the JavaScript setInterval() method You can use the JavaScript setInterval() method to execute a function repeatedly after a certain time period. The setInterval() method requires two parameters first one is typically a function or an expression and the other is time delay in milliseconds.
The setInterval() method calls a function at specified intervals (in milliseconds). The setInterval() method continues calling the function until clearInterval() is called, or the window is closed. 1 second = 1000 milliseconds.
setInterval() The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.
You must pass a function reference to both setTimeout()
and setInterval()
. That means you pass a function name without the ()
after it or you pass an anonymous function.
When you include the ()
after the function name as in func()
, you are executing the function immediately and then passing the return result to setInterval()
or to setTimeout()
. Unless the function itself returns another function reference, this will never do what you want. This is a very common mistake for Javascript programmers (I made the same mistake myself when learning the language).
So, the correct code is:
setTimeout(func, 1500);
setInterval(func, 1500);
If you know other languages that use pointers, you can think of the name of a function with the ()
after it as like a pointer to the function and that's what a function reference is in Javascript and that's what you pass to a function when you want it to be able to execute some function later, not immediately.
When you mistakenly do this:
setTimeout(func(), 1500);
setInterval(func(), 1500);
It is executing your func()
immediately and then passing the return result to setTimeout()
and setInterval()
. Since your func()
doesn't return anything, you are essentially doing this:
func();
setTimeout(undefined, 1500);
Which is what you observe happening, but not what you want.
Further, if you want to execute a specific function call such as console.log("Bowties are cool.")
, then you can wrap it in another function like this:
setTimeout(function() {
console.log("Bowties are cool.")
}, 1500);
So, again you are passing a function reference to setTimeout()
that can be executed LATER rather than executing it immediately which is what you were doing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With