Loading ...
Sorry, an error occurred while loading the content.

Using setTimout and setInterval

Expand Messages
  • Alex
    Hey guys, I ve had this silly bug with setTimeout today where I put this line of code: setTimeout(getDatesOfWeek(), 1000); So I reference the function and
    Message 1 of 7 , Dec 30, 2010
    • 0 Attachment
      Hey guys,

      I've had this silly bug with setTimeout today where I put this line of code:

      setTimeout(getDatesOfWeek(), 1000);

      So I reference the function and expect it to do it's thing every 1 second, but it only did it 1 time.
      From websites explaining the use of setTimeout and setInterval they all show this:

      setTimeout("getDatesOfWeek()", 1000);

      So when JSLint pops an error for that line, saying it's evil or what ever, and you should remove the "", I just removed them, but lost my functionality. Well, actually, it didn't work as intended with the "" also, but that's another issue.

      Question is, Since JSLint and FireBug console don't show an error on my original line, how come putting a function (method?) call in setTimeout is valid code? And if it's not, maybe JSLint should worn about it.

      Thanks.
    • Mark Volkmann
      ... Try this: setTimeout(getDatesOfWeek, 1000); Not that this passes the function getDatesOfWeek to setTimeout rather than passing the result of invoking it.
      Message 2 of 7 , Dec 30, 2010
      • 0 Attachment
        On Thu, Dec 30, 2010 at 3:02 PM, Alex <thespineproject@...> wrote:

        >
        >
        > Hey guys,
        >
        > I've had this silly bug with setTimeout today where I put this line of
        > code:
        >
        > setTimeout(getDatesOfWeek(), 1000);
        >
        > So I reference the function and expect it to do it's thing every 1 second,
        > but it only did it 1 time.
        > From websites explaining the use of setTimeout and setInterval they all
        > show this:
        >
        > setTimeout("getDatesOfWeek()", 1000);
        >
        > So when JSLint pops an error for that line, saying it's evil or what ever,
        > and you should remove the "", I just removed them, but lost my
        > functionality. Well, actually, it didn't work as intended with the "" also,
        > but that's another issue.
        >
        > Question is, Since JSLint and FireBug console don't show an error on my
        > original line, how come putting a function (method?) call in setTimeout is
        > valid code? And if it's not, maybe JSLint should worn about it.
        >

        Try this:

        setTimeout(getDatesOfWeek, 1000);

        Not that this passes the function getDatesOfWeek to setTimeout
        rather than passing the result of invoking it.

        --
        R. Mark Volkmann
        Object Computing, Inc.


        [Non-text portions of this message have been removed]
      • Alex
        Oops, I forgot to mention that was after I found the solution which is to remove the (). But the question still stand as for how come it s valid and doesn t
        Message 3 of 7 , Dec 30, 2010
        • 0 Attachment
          Oops, I forgot to mention that was after I found the solution which is to remove the ().

          But the question still stand as for how come it's valid and doesn't pop an error.

          --- In jslint_com@yahoogroups.com, Mark Volkmann <r.mark.volkmann@...> wrote:
          >
          > On Thu, Dec 30, 2010 at 3:02 PM, Alex <thespineproject@...> wrote:
          >
          > >
          > >
          > > Hey guys,
          > >
          > > I've had this silly bug with setTimeout today where I put this line of
          > > code:
          > >
          > > setTimeout(getDatesOfWeek(), 1000);
          > >
          > > So I reference the function and expect it to do it's thing every 1 second,
          > > but it only did it 1 time.
          > > From websites explaining the use of setTimeout and setInterval they all
          > > show this:
          > >
          > > setTimeout("getDatesOfWeek()", 1000);
          > >
          > > So when JSLint pops an error for that line, saying it's evil or what ever,
          > > and you should remove the "", I just removed them, but lost my
          > > functionality. Well, actually, it didn't work as intended with the "" also,
          > > but that's another issue.
          > >
          > > Question is, Since JSLint and FireBug console don't show an error on my
          > > original line, how come putting a function (method?) call in setTimeout is
          > > valid code? And if it's not, maybe JSLint should worn about it.
          > >
          >
          > Try this:
          >
          > setTimeout(getDatesOfWeek, 1000);
          >
          > Not that this passes the function getDatesOfWeek to setTimeout
          > rather than passing the result of invoking it.
          >
          > --
          > R. Mark Volkmann
          > Object Computing, Inc.
          >
          >
          > [Non-text portions of this message have been removed]
          >
        • Joshua Bell
          ... Consider: function make_callback(arg) { console.log( making a new callback function... ); var cb = function() { console.log( callback called: , 123); };
          Message 4 of 7 , Dec 30, 2010
          • 0 Attachment
            On Thu, Dec 30, 2010 at 2:13 PM, Alex <thespineproject@...> wrote:

            > Oops, I forgot to mention that was after I found the solution which is to
            > remove the ().
            >
            > But the question still stand as for how come it's valid and doesn't pop an
            > error.
            >

            Consider:

            function make_callback(arg) {
            console.log("making a new callback function...");
            var cb = function() {
            console.log("callback called: ", 123);
            };
            return cb;
            }

            Compare the difference in behavior between this:

            >> setTimeout(make_callback, 1000);
            <<one second passes>>
            making a new callback function...

            And this:

            >> setTimeout(make_callback("blah"), 1000);
            making a new callback function...
            <<one second passes>>
            callback called: blah

            In the first case, you're passing a function reference setTimeout (to call
            later). In the second case, you're evaluating a function and passing the
            return value (hopefully a function reference) to setTimeout. Both are valid
            use cases of setTimeout. In this example, passing the function reference
            directly (the first case) is probably not what was intended since the newly
            created callback function is simply discarded.

            ...

            To stem the flow of questions of this sort to the JSLint group, would it
            make sense to have each JSLint warning/ error include a hyperlink to a page
            (which might be a Wiki document) with the collected wisdom of the group on
            how to address the error? (Or possibly just a link to a particular part of
            the http://www.jslint.com/lint.html page that details the error case and
            describes how to disable the option (if necessary)?


            [Non-text portions of this message have been removed]
          • Erik Eckhardt
            It s not an error because setTimeout expects a function as the first parameter. However, if the first parameter is of type string, presumably it wraps it in an
            Message 5 of 7 , Dec 30, 2010
            • 0 Attachment
              It's not an error because setTimeout expects a function as the first
              parameter. However, if the first parameter is of type string, presumably it
              wraps it in an anoymous function as in `new Function('invocation()');`

              It is a solid and core concept of javascript that a function variable is
              invoked with parentheses and is only referenced without them.

              function returnafunction() {
              ___return function(a, b) {return a + b;};
              }

              function invokeafunction(fn) {
              ___fn();
              }

              x = returnafunction();
              invokeafunction(x);

              On Thu, Dec 30, 2010 at 2:13 PM, Alex <thespineproject@...> wrote:

              >
              >
              > Oops, I forgot to mention that was after I found the solution which is to
              > remove the ().
              >
              > But the question still stand as for how come it's valid and doesn't pop an
              > error.
              >
              > --- In jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com>, Mark
              > Volkmann <r.mark.volkmann@...> wrote:
              >
              > >
              > > On Thu, Dec 30, 2010 at 3:02 PM, Alex <thespineproject@...> wrote:
              > >
              > > >
              > > >
              > > > Hey guys,
              > > >
              > > > I've had this silly bug with setTimeout today where I put this line of
              > > > code:
              > > >
              > > > setTimeout(getDatesOfWeek(), 1000);
              > > >
              > > > So I reference the function and expect it to do it's thing every 1
              > second,
              > > > but it only did it 1 time.
              > > > From websites explaining the use of setTimeout and setInterval they all
              > > > show this:
              > > >
              > > > setTimeout("getDatesOfWeek()", 1000);
              > > >
              > > > So when JSLint pops an error for that line, saying it's evil or what
              > ever,
              > > > and you should remove the "", I just removed them, but lost my
              > > > functionality. Well, actually, it didn't work as intended with the ""
              > also,
              > > > but that's another issue.
              > > >
              > > > Question is, Since JSLint and FireBug console don't show an error on my
              > > > original line, how come putting a function (method?) call in setTimeout
              > is
              > > > valid code? And if it's not, maybe JSLint should worn about it.
              > > >
              > >
              > > Try this:
              > >
              > > setTimeout(getDatesOfWeek, 1000);
              > >
              > > Not that this passes the function getDatesOfWeek to setTimeout
              > > rather than passing the result of invoking it.
              > >
              > > --
              > > R. Mark Volkmann
              > > Object Computing, Inc.
              > >
              > >
              > > [Non-text portions of this message have been removed]
              > >
              >
              >
              >


              [Non-text portions of this message have been removed]
            • Alex
              Thanks for the thorough explanation, it seems more logical now. But I still can t get my head around it... Here s my test code: function getDatesOfWeek() { var
              Message 6 of 7 , Dec 30, 2010
              • 0 Attachment
                Thanks for the thorough explanation, it seems more logical now.
                But I still can't get my head around it...

                Here's my test code:

                function getDatesOfWeek() {
                var currentDate = new Date(),
                theTime = currentDate.getSeconds();
                document.body.innerHTML = theTime;
                setTimeout(getDatesOfWeek(), 1000);
                }
                getDatesOfWeek();

                Which does not update the seconds. But if I remove the () or put "getDatesOfWeek()", it does work. Which bugs me even more cause I understood from what you wrote that if I put "" then it's just like invoking the getDatesOfWeek(), which should give the same result?

                If you can explain these 3 cases, I'll be set :-).

                --- In jslint_com@yahoogroups.com, Joshua Bell <josh@...> wrote:
                >
                > On Thu, Dec 30, 2010 at 2:13 PM, Alex <thespineproject@...> wrote:
                >
                > > Oops, I forgot to mention that was after I found the solution which is to
                > > remove the ().
                > >
                > > But the question still stand as for how come it's valid and doesn't pop an
                > > error.
                > >
                >
                > Consider:
                >
                > function make_callback(arg) {
                > console.log("making a new callback function...");
                > var cb = function() {
                > console.log("callback called: ", 123);
                > };
                > return cb;
                > }
                >
                > Compare the difference in behavior between this:
                >
                > >> setTimeout(make_callback, 1000);
                > <<one second passes>>
                > making a new callback function...
                >
                > And this:
                >
                > >> setTimeout(make_callback("blah"), 1000);
                > making a new callback function...
                > <<one second passes>>
                > callback called: blah
                >
                > In the first case, you're passing a function reference setTimeout (to call
                > later). In the second case, you're evaluating a function and passing the
                > return value (hopefully a function reference) to setTimeout. Both are valid
                > use cases of setTimeout. In this example, passing the function reference
                > directly (the first case) is probably not what was intended since the newly
                > created callback function is simply discarded.
                >
                > ...
                >
                > To stem the flow of questions of this sort to the JSLint group, would it
                > make sense to have each JSLint warning/ error include a hyperlink to a page
                > (which might be a Wiki document) with the collected wisdom of the group on
                > how to address the error? (Or possibly just a link to a particular part of
                > the http://www.jslint.com/lint.html page that details the error case and
                > describes how to disable the option (if necessary)?
                >
                >
                > [Non-text portions of this message have been removed]
                >
              • Mark Volkmann
                I looks like you want to update the seconds every 1000 ms. I think you want this: function updateSeconds() { var currentDate = new Date();
                Message 7 of 7 , Dec 31, 2010
                • 0 Attachment
                  I looks like you want to update the seconds every 1000 ms. I think you want
                  this:

                  function updateSeconds() {
                  var currentDate = new Date();
                  document.body.innerHTML = currentDate.getSeconds();
                  }

                  setInterval(updateSeconds, 1000);

                  On Thu, Dec 30, 2010 at 7:41 PM, Alex <thespineproject@...> wrote:

                  >
                  >
                  > Thanks for the thorough explanation, it seems more logical now.
                  > But I still can't get my head around it...
                  >
                  > Here's my test code:
                  >
                  > function getDatesOfWeek() {
                  > var currentDate = new Date(),
                  > theTime = currentDate.getSeconds();
                  > document.body.innerHTML = theTime;
                  > setTimeout(getDatesOfWeek(), 1000);
                  > }
                  > getDatesOfWeek();
                  >
                  > Which does not update the seconds. But if I remove the () or put
                  > "getDatesOfWeek()", it does work. Which bugs me even more cause I understood
                  > from what you wrote that if I put "" then it's just like invoking the
                  > getDatesOfWeek(), which should give the same result?
                  >
                  > If you can explain these 3 cases, I'll be set :-).
                  >
                  > --- In jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com>, Joshua
                  > Bell <josh@...> wrote:
                  > >
                  > > On Thu, Dec 30, 2010 at 2:13 PM, Alex <thespineproject@...> wrote:
                  > >
                  > > > Oops, I forgot to mention that was after I found the solution which is
                  > to
                  > > > remove the ().
                  > > >
                  > > > But the question still stand as for how come it's valid and doesn't pop
                  > an
                  > > > error.
                  > > >
                  > >
                  > > Consider:
                  > >
                  > > function make_callback(arg) {
                  > > console.log("making a new callback function...");
                  > > var cb = function() {
                  > > console.log("callback called: ", 123);
                  > > };
                  > > return cb;
                  > > }
                  > >
                  > > Compare the difference in behavior between this:
                  > >
                  > > >> setTimeout(make_callback, 1000);
                  > > <<one second passes>>
                  > > making a new callback function...
                  > >
                  > > And this:
                  > >
                  > > >> setTimeout(make_callback("blah"), 1000);
                  > > making a new callback function...
                  > > <<one second passes>>
                  > > callback called: blah
                  > >
                  > > In the first case, you're passing a function reference setTimeout (to
                  > call
                  > > later). In the second case, you're evaluating a function and passing the
                  > > return value (hopefully a function reference) to setTimeout. Both are
                  > valid
                  > > use cases of setTimeout. In this example, passing the function reference
                  > > directly (the first case) is probably not what was intended since the
                  > newly
                  > > created callback function is simply discarded.
                  > >
                  > > ...
                  > >
                  > > To stem the flow of questions of this sort to the JSLint group, would it
                  > > make sense to have each JSLint warning/ error include a hyperlink to a
                  > page
                  > > (which might be a Wiki document) with the collected wisdom of the group
                  > on
                  > > how to address the error? (Or possibly just a link to a particular part
                  > of
                  > > the http://www.jslint.com/lint.html page that details the error case and
                  > > describes how to disable the option (if necessary)?
                  > >
                  > >
                  > > [Non-text portions of this message have been removed]
                  > >
                  >
                  >
                  >



                  --
                  R. Mark Volkmann
                  Object Computing, Inc.


                  [Non-text portions of this message have been removed]
                Your message has been successfully submitted and would be delivered to recipients shortly.