473,769 Members | 6,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setTimeout() syntax

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

window.setTimeo ut(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.setTimeo ut("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
6 5011
On 6. Máj, 10:14 h., Steve <stephen.jo...@ googlemail.comw rote:
I have noticed that setTimeout works if the brackets () are left off
the function called, like this:

window.setTimeo ut(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.setTimeo ut("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(func tion() { 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
On May 6, 11:41 am, "P. Prikryl" <p.prik...@gmai l.comwrote:
On 6. Máj, 10:14 h., Steve <stephen.jo...@ googlemail.comw rote:I havenoticed that setTimeout works if the brackets () are left off the function called, like this:
window.setTimeo ut(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
Steve wrote:
On May 6, 11:41 am, "P. Prikryl" <p.prik...@gmai l.comwrote:
>On 6. Máj, 10:14 h., Steve <stephen.jo...@ googlemail.comw rote:I have noticed that setTimeout works if the brackets () are left off the function called, like this:
>>window.setTim eout(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(hell o, 1000);
will call hello() after 1000ms has passed, and show you the alert dialog
with text "hello".

setTimeout(hell o(), 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
On May 6, 12:08 pm, Tuomo Tanskanen wrote:
<snip>
Example:
function hello() { alert("hello"); return true; }
<snip>
setTimeout(hell o(), 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 ExpressionState ment
(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 ExpressionState ment.
Jun 27 '08 #5
On May 6, 1:33 pm, Henry <rcornf...@rain drop.co.ukwrote :
On May 6, 12:08 pm, Tuomo Tanskanen wrote:
<snip>
Example:
function hello() { alert("hello"); return true; }
<snip>
setTimeout(hell o(), 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 ExpressionState ment
(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 ExpressionState ment.
Thanks guys!
Jun 27 '08 #6
Henry wrote:
On May 6, 12:08 pm, Tuomo Tanskanen wrote:
<snip>
>Example:
function hello() { alert("hello"); return true; }
<snip>
>setTimeout(hel lo(), 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 ExpressionState ment
(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 ExpressionState ment.
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2598
by: Robert Mark Bram | last post by:
Howdy All! How can I make something like this work? var message = "This will appear in 10 seconds"; setTimeout ('alert(message)', 10000); Thanks for any advice!
3
14949
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
29
8781
by: Mic | last post by:
Goal: delay execution of form submit Code (Javascript + JScript ASP): <% Response.Write("<OBJECT ID='IntraLaunch' STYLE='display : none' WIDTH=0 HEIGHT=0 CLASSID='CLSID:0AE533FE-B805-4FD6-8AE1-A619FBEE7A23' CODEBASE='IntraLaunch.CAB#version=5,0,0,3'>") Response.Write("<PARAM NAME='ImageLoc' VALUE='Null'>") Response.Write("<PARAM NAME='ImageSrc' VALUE='Null'>")
2
7692
by: JellyON | last post by:
Hi. I'm a little bit lost trying to insert a setTimeout() for recursive call containing string and numeric parameters. Here is a generic example of what I would like to succeed to do : function Stuff(a_str, an_int, delay) { // Three useful vars for managing the recursivity var stop = false; // will be true when no more recurse needed var command; // just to concatenate the recurse expression
3
8595
by: jshanman | last post by:
I've created a "Play" button for my timeline that uses the Google Maps API. It basicly scrolls the timeline x interval every y milliseconds. The problem is the browser responds slowly even after the "Stop" button is clicked. (it responds slowly while playing, but thats to be expected with everything that it is doing) How can I get the browser back to it's more responsive state after the timeout is cleared? Here is the function that...
15
3796
by: nikki_herring | last post by:
I am using setTimeout( ) to continuously call a function that randomly rotates/displays 2 images on a page. The part I need help with is the second image should rotate 3 seconds after the first image rotates. I cannot figure out how to accomplish the 3 second delay. My code is pasted below: function randPic(){ randPic1(); randPic2();
2
2010
by: M. Fisher | last post by:
I have a line in my html file as following: <div id="selectBox" class="selectbox" onclick="$ ('PostMark_Pa4440a_SelectCol_1').toggle();" onmouseout="setTimeout('$ ('PostMark_Pa4440a_SelectCol_1').hide();', 10000);">Select...</div> But because I have to use those single quotes as you see, I get the following error. Error: missing ) after argument list
1
14047
by: alex.malgaroli | last post by:
Hi all. I didn't find anything on the web regarding this different behaviour I found in Firefox (v. 2) and IE (v. 6), so I'm posting it here, maybe someone will benefit from this. I have a function in a javascript object like this: function MyObject.prototype = { myvar = "This is myvar"; myfunction(myarg) {
3
1524
tpgames
by: tpgames | last post by:
I can't get this script to recognize that the image name is Image53.jpg, Image54.jpg, etc. instead of 53.jpg, 54.jpg. What am I doing wrong? I'm trying to not have to change a thousand image names. :D //change x=0 to x=1 change current= 0 to current = 1 Works! // The 1 = the first image number for both X and Current! imgName = new Array() for (x=53; x<=100; x++) {
0
9590
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9424
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10051
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.