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
  • 42,611
  • 64
  • 162
  • 244
  • 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
  • 27,479
  • 9
  • 75
  • 76
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
  • 914,110
  • 126
  • 1,211
  • 1,335