This question is usually asked first regarding setTimeout
, but I think it is important to indicate that the behavior here is not specific to this function. You just need to understand what the brackets do and what it means to leave them.
Assume the following function:
function foo() { return 5; }
Consider the following two variable declarations / assignments:
var one = foo(); var two = foo;
What values do these variables have?
In the first case, we execute the function foo
and assign its return value - the number 5
- to one
. In the second case, we assign foo
ourselves - more precisely, a link to foo
- to two
. The function is never executed.
With this knowledge and understanding that setTimeout
expects function references as its first argument, it should be obvious why your first case fails, but the second works.
Of course, your problem is compounded by the fact that the function you are performing is a recursive call for yourself. This will be done forever - for some definition forever - because there is no base case for completing a recursion.
source share