0

Why when passing the code (surrounded by '') like so:

setTimeout('alert("James Blunt is bad")', 5000);

Is there the 5 second pause before showing what we all know and when passing function reference like so:

setTimeout(alert("James Blunt is bad"), 5000);

There is no pause?

http://jsfiddle.net/eBcpc/

rogermushroom
  • 5,486
  • 4
  • 42
  • 68
  • possible duplicate of [setTimeout won't wait, even when waiting to call a function](http://stackoverflow.com/questions/8625684/settimeout-wont-wait-even-when-waiting-to-call-a-function) – Felix Kling Jan 19 '12 at 18:15
  • Dup of [setTimeOut() is not working with AJAX](http://stackoverflow.com/questions/1447897/), [Issue with setTimeout in ajax based polling using jquery](http://stackoverflow.com/questions/1855136/), [Why is my function call that should be scheduled by setTimeout executed immediately?](http://stackoverflow.com/questions/2037203/), [What's the differenct between passing a function and the function call itself in javascript?](http://stackoverflow.com/questions/2944207/) – outis Jan 20 '12 at 10:41

3 Answers3

3

If your second example, you are not passing a function.

You are calling alert and passing its return value (and the return value of alert is not a function).

Function references don't end in (…)

setTimeout(function () { alert("Hello, world"); }, 5000);

Or, with weaker browser support, pass the arguments to alert in an array as the third argument to setTimeout.:

setTimeout(alert, 5000, ["Hello, world"]);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    You beat me too it, good answer. The only thing I would add is maybe this reference - https://developer.mozilla.org/en/DOM/window.setTimeout. They explain it well, too. – shanabus Jan 19 '12 at 17:55
  • DOH, So obvious now you have said. – rogermushroom Jan 19 '12 at 17:56
  • +1 for adding *"and the return value of alert is not a function"*. Most people leave that out, but it's an important part of understanding why the result was unexpected. – Wayne Jan 19 '12 at 18:27
1

When you call setTimeout with a code argument, you have to wrap it in a function:

setTimeout(function () { alert("James Blunt is bad"); }, 5000);

Otherwise, when JavaScript executes setTimeout, it will run the alert function instantaneously in the hopes that the alert will return a function object which can then be enqueued onto the timeout queue. (Obviously, alert will not return a function.)

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
1

In the second example, you are invoking the alert function, then calling setTimeout. This is because the arguments to a function are processed before the function is called. (If alert had a return value and you wanted to pass that value as an argument to another function, then this is exactly the behavior you would want.)

What you want to do is pass an anonymous function to setTimeout, then have that anonymous function call alert, like so:

setTimeout(function() {
        alert("James Blunt is bad");
    }, 5000);
dgvid
  • 26,293
  • 5
  • 40
  • 57