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

Re: [jslint] Using setTimout and setInterval

Expand Messages
  • 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 1 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 2 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.