You have to pass the function as a parameter to setTimeout
, that means no parenthesis at the end of f
. What you are doing right now is calling f
and passing it's return value to setTimeout
. You can pass arguments to f
as the third argument of setTimeout
. Your call should look like this:
setTimeout(f, 2000, attempt + 1);