|
Prev: background image
Next: Dynamic form processing
From: Steve on 6 May 2008 04:14 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 :-)
From: P. Prikryl on 6 May 2008 05:41 On 6. Máj, 10:14 h., Steve <stephen.jo...(a)googlemail.com> wrote: > 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 :-)
From: Steve on 6 May 2008 06:58 On May 6, 11:41 am, "P. Prikryl" <p.prik...(a)gmail.com> wrote: > On 6. Máj, 10:14 h., Steve <stephen.jo...(a)googlemail.com> wrote:> 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?
From: Tuomo Tanskanen on 6 May 2008 07:08 Steve wrote: > On May 6, 11:41 am, "P. Prikryl" <p.prik...(a)gmail.com> wrote: >> On 6. M�j, 10:14 h., Steve <stephen.jo...(a)googlemail.com> wrote:> 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
From: Henry on 6 May 2008 07:33
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. |