0

This doesn't seem right, but this is how Firefox appears to be acting.

setTimeout(print(),5000);
function print(){
    console.log(1);
}

Before the 5 seconds are up, after a link is clicked and before the next page begins to render, 1 is printed to the console. Any ideas? Thanks!

Edit: retitled, to make it obvious that there is a bug in the example code, not in firefox behavior.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
user1144112
  • 471
  • 1
  • 4
  • 5
  • For what it's worth, 50000 milliseconds is 50 seconds, not 5. – lonesomeday Jan 11 '12 at 21:00
  • possible duplicate of [Why is the method executed immediately when I use setTimeout?](http://stackoverflow.com/questions/7137401/why-is-the-method-executed-immediately-when-i-use-settimeout) – Brock Adams Jan 14 '12 at 10:04

2 Answers2

7

No, the problem is that you are executing the print function immediately. Remove the brackets so you pass the function object rather than execute it.

setTimeout(print,50000);
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
4

Firefox will call print when it gets an expression that calls it, such as print().

You are calling print and passing its return value (undefined) to setTimeout.

Drop the () to pass the function itself.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335