[ACCEPTED]-Loading message and setTimeout-jquery

Accepted answer
Score: 10

You need to replace:

setTimeout("simulate_ajax_call()", 5000);

with:

setTimeout(simulate_ajax_call, 5000);

Check out the working example


You should avoid 2 putting () at the end of the function name 1 because otherwise it gets called/run immediately :)

Score: 4

You need to drop the quotes and the parenthesis.

Doing setTimeout("simulate_ajax_call()", 5000) is equivalent of eval()ing that code, which 1 is automatically running the function.

Score: 1

setTimeout evals its string argument in the global scope, but 12 simulate_ajax_call is declared in a nested scope, so the eval can't 11 find it in order to invoke it.

As the other 10 two answers point out the best way to resolve 9 the issue is to use the function-passing 8 version of setTimeout, but being careful to remove 7 the parentheses, because you don't want 6 to accidentally invoke the function immediately.

Your 5 code would probably have worked had you 4 declared simulate_ajax_call at the global scope. The parentheses 3 would have been correct in this case, although 2 the string version of setTimeout is still a bad idea 1 in general.

More Related questions