1

The following prints my message straight away

setTimeout(console.log('delayed hello world'), 10000);

It's a little bit counterintuitive. And since my message print straight away what happens at the end of 10 seconds?

deltanovember
  • 40,595
  • 61
  • 158
  • 240
  • 3
    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) and [so many others](http://stackoverflow.com/search?q=why+does+settimeout+execute+the+function+immediately). – Felix Kling May 11 '12 at 09:01

2 Answers2

5

You need to use anonymously function for that:

setTimeout(function() { console.log('delayed hello world') }, 10000);

See more about passing params to setTimeout function at MDN

antyrat
  • 26,950
  • 9
  • 73
  • 75
4

You are running console.log (because you have () on the end of it) and passing its return value to setTimeout instead of passing a function.

var myFunction = function () { console.log('delayed hello world'); }
setTimeout(myFunction, 10000);
Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264