setTimeout() syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    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 :-)

  • P. Prikryl

    #2
    Re: setTimeout() syntax

    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 :-)

    Comment

    • Steve

      #3
      Re: setTimeout() syntax

      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?




      Comment

      • Tuomo Tanskanen

        #4
        Re: setTimeout() syntax

        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

        Comment

        • Henry

          #5
          Re: setTimeout() syntax

          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.

          Comment

          • Steve

            #6
            Re: setTimeout() syntax

            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!

            Comment

            • Tuomo Tanskanen

              #7
              Re: setTimeout() syntax

              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/

              Comment

              Working...