setTimeout calls function immediately instead of after delay

A function object in JavaScript is one thing. A function call is a different thing. You’re using the latter, by including parentheses after the function name*, but you need the former, without parentheses. This allows setTimeout to later invoke the function itself by using the passed-in object. Assuming you do actually want 5 seconds (rather than the 50 seconds the original code was using):

setTimeout(GetUsersNumber, 5000);

*Really, any old variable that holds a function object can be invoked like that, but for convenience, defining a function also defines a variable name for it.

Leave a Comment