I was playing around with the \"setTimeout\" function. This code runs like expected:
function myFunction() {
console.log(\'test\');
setTimeout(myFunction
myFunction()
calls the function immediately and passes its return value to setTimeout
to be called later (except its return value is undefined
, which isn't a function (or a string), so that is pointless).
That is because by including the parentheses you're actually executing the function and passing its result to setTimeout
.
setTimeout(myFunction(), 1000);
means that your pass result returned by myFunction() as the 1st argument of setTimeout. myFunction returns nothing, so setTimeout(myFunction(), 1000);
is the same as setTimeout(undefined, 1000);