By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,963 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,963 IT Pros & Developers. It's quick & easy.

setTimeout() syntax

P: n/a
I have noticed that setTimeout works if the brackets () are left off
the function called, like this:

window.setTimeout(myFunction, 1000);

I thought that the brackets were required to show that myFunction is a
function, so why are they not required here?

Also I have found that if I want to pass a value to a function with
setTimeout I need to enclose the function in inverted commas, like
this;

window.setTimeout("myFunction(10)", 1000);

Why are the inverted comma's required here but not in the example
above?

I realize that these questions will be self explanatory to some of you
but I cannot find the answers even in the Rhino book. So friendly and
useful answers only please :-)

Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 6. Máj, 10:14 h., Steve <stephen.jo...@googlemail.comwrote:
I have noticed that setTimeout works if the brackets () are left off
the function called, like this:

window.setTimeout(myFunction, 1000);

I thought that the brackets were required to show that myFunction is a
function, so why are they not required here?
The brackets are not optional - there is a difference: Without
brackets, myFunction will be executed. With brackets, the return value
of the myFunction will be used as first argument of setTimeout.
>
Also I have found that if I want to pass a value to a function with
setTimeout I need to enclose the function in inverted commas, like
this;

window.setTimeout("myFunction(10)", 1000);

Why are the inverted comma's required here but not in the example
above?
You can use string that defines function body as first argument for
setTimeout, but there is better (faster and cleaner) way to pass
values. You can use anonymous function:
setTimeout(function() { myFunction(10); }, 1000)
>
I realize that these questions will be self explanatory to some of you
but I cannot find the answers even in the Rhino book. So friendly and
useful answers only please :-)
Jun 27 '08 #2

P: n/a
On May 6, 11:41 am, "P. Prikryl" <p.prik...@gmail.comwrote:
On 6. Máj, 10:14 h., Steve <stephen.jo...@googlemail.comwrote:I havenoticed that setTimeout works if the brackets () are left off the function called, like this:
window.setTimeout(myFunction, 1000);
I thought that the brackets were required to show that myFunction is a
function, so why are they not required here?

The brackets are not optional - there is a difference: Without
brackets, myFunction will be executed. With brackets, the return value
of the myFunction will be used as first argument of setTimeout.
Thanks for the answer, but I am still a bit confused. Usually the
brackets are need to execute (call) a function so why are they not
need here?


Jun 27 '08 #3

P: n/a
Steve wrote:
On May 6, 11:41 am, "P. Prikryl" <p.prik...@gmail.comwrote:
>On 6. Máj, 10:14 h., Steve <stephen.jo...@googlemail.comwrote:I have noticed that setTimeout works if the brackets () are left off the function called, like this:
>>window.setTimeout(myFunction, 1000);
I thought that the brackets were required to show that myFunction is a
function, so why are they not required here?
The brackets are not optional - there is a difference: Without
brackets, myFunction will be executed. With brackets, the return value
of the myFunction will be used as first argument of setTimeout.

Thanks for the answer, but I am still a bit confused. Usually the
brackets are need to execute (call) a function so why are they not
need here?
When you include the brackets, the function is executed right away and
its return value used in setTimeout. Without brackets, the reference to
that function is used in setTimeout, enabling setTimeout to call that
function after the time has passed.

Example:
function hello() { alert("hello"); return true; }

setTimeout(hello, 1000);
will call hello() after 1000ms has passed, and show you the alert dialog
with text "hello".

setTimeout(hello(), 1000);
will call hello() ASAP and show you the alert dialog. Then it will use
the return value true to create the timeout, and when 1000ms has passed,
setTimeout will try to call that "true", which obviously fails.

Regards, Tumi
Jun 27 '08 #4

P: n/a
On May 6, 12:08 pm, Tuomo Tanskanen wrote:
<snip>
Example:
function hello() { alert("hello"); return true; }
<snip>
setTimeout(hello(), 1000);
will call hello() ASAP and show you the alert dialog. Then it
will use the return value true to create the timeout, and when
1000ms has passed, setTimeout will try to call that "true",
which obviously fails.
That is not the case in practice. Whenever the first argument to -
setTimeout - is not a reference to a function (and even when it is a
reference to a function on older browsers which dod not support
function references as the first argument) whatever argument is
provided is type-converted into a string and that string evaluated
after the specified interval (approximately). The boolean value false
type-converts into the string "false", which, when later treated as
javascript source code, is a valid javascript ExpressionStatement
(even if a totally useless one) and so does not fail at all. It just
does nothing useful. Thus the execution of the code string argument by
- setTimeout - does not produce any error, and for ECMA 262 3rd Ed.
implementations even undefined return values would not cause the
setTimeout to produce an error as the undefined value type-converts to
the string 'undefined' which is also a valid ExpressionStatement.
Jun 27 '08 #5

P: n/a
On May 6, 1:33 pm, Henry <rcornf...@raindrop.co.ukwrote:
On May 6, 12:08 pm, Tuomo Tanskanen wrote:
<snip>
Example:
function hello() { alert("hello"); return true; }
<snip>
setTimeout(hello(), 1000);
will call hello() ASAP and show you the alert dialog. Then it
will use the return value true to create the timeout, and when
1000ms has passed, setTimeout will try to call that "true",
which obviously fails.

That is not the case in practice. Whenever the first argument to -
setTimeout - is not a reference to a function (and even when it is a
reference to a function on older browsers which dod not support
function references as the first argument) whatever argument is
provided is type-converted into a string and that string evaluated
after the specified interval (approximately). The boolean value false
type-converts into the string "false", which, when later treated as
javascript source code, is a valid javascript ExpressionStatement
(even if a totally useless one) and so does not fail at all. It just
does nothing useful. Thus the execution of the code string argument by
- setTimeout - does not produce any error, and for ECMA 262 3rd Ed.
implementations even undefined return values would not cause the
setTimeout to produce an error as the undefined value type-converts to
the string 'undefined' which is also a valid ExpressionStatement.
Thanks guys!
Jun 27 '08 #6

P: n/a
Henry wrote:
On May 6, 12:08 pm, Tuomo Tanskanen wrote:
<snip>
>Example:
function hello() { alert("hello"); return true; }
<snip>
>setTimeout(hello(), 1000);
will call hello() ASAP and show you the alert dialog. Then it
will use the return value true to create the timeout, and when
1000ms has passed, setTimeout will try to call that "true",
which obviously fails.

That is not the case in practice. Whenever the first argument to -
setTimeout - is not a reference to a function (and even when it is a
reference to a function on older browsers which dod not support
function references as the first argument) whatever argument is
provided is type-converted into a string and that string evaluated
after the specified interval (approximately). The boolean value false
type-converts into the string "false", which, when later treated as
javascript source code, is a valid javascript ExpressionStatement
(even if a totally useless one) and so does not fail at all. It just
does nothing useful. Thus the execution of the code string argument by
- setTimeout - does not produce any error, and for ECMA 262 3rd Ed.
implementations even undefined return values would not cause the
setTimeout to produce an error as the undefined value type-converts to
the string 'undefined' which is also a valid ExpressionStatement.
Thank you for elaborating on this, I was not accurate enough. I simply
forgot the "" from the first true (but remembered to add them at the
second). And by failing I did not mean failing as runtime error, but
simply "not doing what you wanted it to do".

Regards, Tumi

--
My home at: http://tanskanen.org/
My CV at: http://tanskanen.org/cv/
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.