setInterval

Discussion in 'Javascript' started by Tom de Neef, Sep 2, 2011.

  1. Tom de Neef

    Tom de Neef Guest

    Two customers, using W7 and IE, report that my app does not repond to
    certain menu commands. No other users seem to have a problem.
    I have tracked it down to setInterval()

    My code has a statement in <script type="text/javascript">:
    function setMenuTimer(){
    if (currentMenuIndx==0) {return};
    if (menuTimerSet>0) {clearInterval(menuTimerID)};
    menuTimerID = setInterval(exitMenu,1500); //<<< A
    menuTimerSet = 1
    }

    menuTimerID, menuTimerSet and currentMenuIndx are declared, exitMenu is a
    function.

    I am confused by the setInterval call (A).
    exitMenu is a function and I have now changed the code to
    menuTimerID = setInterval("exitMenu()",1500); //<<< B

    On my system, both implementations seem to work (and have the desired effect
    of hiding the menu drop down awhile after the cursor moves out of it).
    Questions:
    - is statement A valid? It doesn't throw an error, but should it actually
    call the function exitMenu?
    - if not: how come that the desired functionality seems to be obtained on
    most systems and not on some
    - is statement B the correct way to call setInterval?

    TIA
    Tom
    Tom de Neef, Sep 2, 2011
    #1
    1. Advertising

  2. On Sep 2, 4:20 pm, Tom de Neef wrote:
    > Two customers, using W7 and IE,


    But which IE?

    > report that my app does not
    > repond to certain menu commands. No other users seem to
    > have a problem. I have tracked it down to setInterval()
    >
    > My code has a statement in <script type="text/javascript">:
    > function setMenuTimer(){
    > if (currentMenuIndx==0) {return};
    > if (menuTimerSet>0) {clearInterval(menuTimerID)};
    > menuTimerID = setInterval(exitMenu,1500); //<<< A
    > menuTimerSet = 1
    >
    > }
    >
    > menuTimerID, menuTimerSet and currentMenuIndx are declared,
    > exitMenu is a function.
    >
    > I am confused by the setInterval call (A).
    > exitMenu is a function and I have now changed the code to
    > menuTimerID = setInterval("exitMenu()",1500); //<<< B
    >
    > On my system, both implementations seem to work (and have the
    > desired effect of hiding the menu drop down awhile after the
    > cursor moves out of it).
    > Questions:
    > - is statement A valid?


    Syntactically it is fine.

    > It doesn't throw an error, but should it actually
    > call the function exitMenu?


    If the creation/definition of - exitMenu - satisfies a set of criteria
    not evidenced above then it should have called the function. Using
    function references as the first argument to setInterval/setTimeout
    was not supported on, for example, (and to the best of my
    recollection) Netscape 2, IE 4, Safari 1 and Konqueror 2, but these
    days support exists in every scriptable browser your code is likely to
    encounter.

    > - if not: how come that the desired functionality seems to be
    > obtained on most systems and not on some


    The answer to that would require identifying (by which I mean actually
    pinning down) the cause and effect relationship involved. At minimum,
    that would require putting someone with appropriate analytical skills
    in a position to reproduce the issue.

    > - is statement B the correct way to call setInterval?


    Not to the exclusion of A (they are both 'correct', all else being
    equal), and style-wise A would generally be preferred over B these
    days.

    Richard.
    Richard Cornford, Sep 2, 2011
    #2
    1. Advertising

  3. On Sep 2, 4:20 pm, Tom de Neef wrote:
    > Two customers, using W7 and IE,


    But which IE?

    > report that my app does not
    > repond to certain menu commands. No other users seem to
    > have a problem. I have tracked it down to setInterval()
    >
    > My code has a statement in <script type="text/javascript">:
    > function setMenuTimer(){
    > if (currentMenuIndx==0) {return};
    > if (menuTimerSet>0) {clearInterval(menuTimerID)};
    > menuTimerID = setInterval(exitMenu,1500); //<<< A
    > menuTimerSet = 1
    >
    > }
    >
    > menuTimerID, menuTimerSet and currentMenuIndx are declared,
    > exitMenu is a function.
    >
    > I am confused by the setInterval call (A).
    > exitMenu is a function and I have now changed the code to
    > menuTimerID = setInterval("exitMenu()",1500); //<<< B
    >
    > On my system, both implementations seem to work (and have the
    > desired effect of hiding the menu drop down awhile after the
    > cursor moves out of it).
    > Questions:
    > - is statement A valid?


    Syntactically it is fine.

    > It doesn't throw an error, but should it actually
    > call the function exitMenu?


    If the creation/definition of - exitMenu - satisfies a set of criteria
    not evidenced above then it should have called the function. Using
    function references as the first argument to setInterval/setTimeout
    was not supported on, for example, (and to the best of my
    recollection) Netscape 2, IE 4, Safari 1 and Konqueror 2, but these
    days support exists in every scriptable browser you code is likely to
    encounter.

    > - if not: how come that the desired functionality seems to be
    > obtained on most systems and not on some


    The answer to that would require identifying (by which I mean actually
    pinning down) the cause and effect relationship involved. At minimum,
    that would require putting someone with appropriate analytical skills
    in a position to reproduce the issue.

    > - is statement B the correct way to call setInterval?


    Not to the exclusion of A (they are both 'correct', all else being
    equal), and style-wise A would generally be preferred over B these
    days.

    Richard.
    Richard Cornford, Sep 2, 2011
    #3
  4. In comp.lang.javascript message <4e60f427$0$2537$4
    all.nl>, Fri, 2 Sep 2011 17:20:06, Tom de Neef <>
    posted:

    >Two customers, using W7 and IE, report that my app does not repond to
    >certain menu commands. No other users seem to have a problem.
    >I have tracked it down to setInterval()


    Pragmatically, you could try using repeated setTimeout instead. It
    might work better.

    My own site uses 54 "setTimeout(", but 10 "setInterval(" in files
    js*.htm, and I've had no relevant adverse comment.

    --
    (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
    Website <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
    Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
    Dr J R Stockton, Sep 3, 2011
    #4
  5. Tom de Neef

    Tom de Neef Guest

    "Thomas 'PointedEars' Lahn" <> schreef in bericht
    news:...
    > Tom de Neef wrote:
    >
    >> Two customers, using W7 and IE, report that my app does not repond to
    >> certain menu commands. No other users seem to have a problem.

    >
    > Your problem is likely caused by how many times setMenuTimer() is called,
    > as
    > intervals would accumulate if it is called more than one time.
    > window.setInterval() is almost always inferior to a self-calling
    > window.setTimeout().
    >
    >> My code has a statement in <script type="text/javascript">:
    >> function setMenuTimer(){
    >> if (currentMenuIndx==0) {return};
    >> if (menuTimerSet>0) {clearInterval(menuTimerID)};

    >
    > You should not rely on this test; there is nothing that says that
    > setInterval() cannot return 0, or values less than 0. Instead, initialize
    > the timer variable with a value that is rather unlikely to be returned by
    > window.setInterval(), such as `null', and compare against that. After
    > window.clearInterval(), always reset the variable value.


    You may have missed that I used menuTimerSet (0 or 1) for the test and
    menuTimerID for the return value of the setInterval call.
    I think that was the correct way of doing it, but I liked your approach
    better, using 'null' and avoiding the need for the test variable.

    >
    >> menuTimerID = setInterval(exitMenu,1500); //<<< A
    >> menuTimerSet = 1
    >> }
    >>
    >> menuTimerID, menuTimerSet and currentMenuIndx are declared, exitMenu is a
    >> function.

    >
    > No, it is the name in a function declaration, or the name in a variable
    > declaration for a variable that is later assigned a function reference; so
    > it is a function *reference*. Functions are first-class objects in-
    > ECMAScript implementations. Objects need be referred to.


    Always a pleasure to read your formal corrections. I learn from it but I
    fear I'm too old to catch up.

    >> I am confused by the setInterval call (A).
    >> exitMenu is a function and I have now changed the code to
    >> menuTimerID = setInterval("exitMenu()",1500); //<<< B
    >>
    >> On my system, both implementations seem to work (and have the desired
    >> effect of hiding the menu drop down awhile after the cursor moves out -
    >> it). Questions:
    >> - is statement A valid? It doesn't throw an error, but should it actually
    >> call the function exitMenu?
    >> [.]
    >> - is statement B the correct way to call setInterval?

    >
    > Yes, and it depends (`exitMenu' has to be global for the latter to work).
    > It should be window.setInterval(.), though.


    Yes.

    Thank you and Richard C and Dr S.
    I have moved from window.setInterval to window.setTimeout. The code is
    relatively simple and I am sure that setTimeout will not be called during
    its own wait period.
    When I step through the code at the client side (using W7 RemoteHelp)
    evrything works fine. But letting the code run freely leads to
    irreponsiveness (with this customer only).
    There must be another timing issue - outside my own code - to do with Ajax
    calls to the server. I think that my worry about setInterval has been a red
    herring.
    Tom de Neef
    Tom de Neef, Sep 5, 2011
    #5
  6. Tom de Neef wrote:

    > "Thomas 'PointedEars' Lahn" [wrote:]
    >> Tom de Neef wrote:
    >>> My code has a statement in <script type="text/javascript">:
    >>> function setMenuTimer(){
    >>> if (currentMenuIndx==0) {return};
    >>> if (menuTimerSet>0) {clearInterval(menuTimerID)};

    >> You should not rely on this test; there is nothing that says that
    >> setInterval() cannot return 0, or values less than 0. Instead,
    >> initialize the timer variable with a value that is rather unlikely to be
    >> returned by window.setInterval(), such as `null', and compare against
    >> that. After window.clearInterval(), always reset the variable value.

    >
    > You may have missed that I used menuTimerSet (0 or 1) for the test and
    > menuTimerID for the return value of the setInterval call.


    Yes, indeed.

    > I think that was the correct way of doing it, but I liked your approach
    > better, using 'null' and avoiding the need for the test variable.


    Yet I think your approach is safer, as we can only *guess* about the return
    value of `setInterval', but we *know* the value of our extra variable.

    Please do not post attribution novels. The attribution should be in a
    single line so that text, especially with several quotation levels, can be
    easily attributed to its author.


    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
    Thomas 'PointedEars' Lahn, Sep 5, 2011
    #6
  7. In comp.lang.javascript message <>,
    Mon, 5 Sep 2011 20:53:25, Thomas 'PointedEars' Lahn <>
    posted:

    >Please do not post attribution novels. The attribution should be in a
    >single line so that text, especially with several quotation levels, can be
    >easily attributed to its author.


    You have no authority to impose such rules. Informative attributions
    are useful under circumstances that you are unwilling to envisage.

    You are not Tim Berners-Lee. and therefore your sigsep is invalid.

    It is not even sound advice, though it may have been when Sir Tim wrote
    it. I can write a page which is best NOT read in current Opera
    (crashes), Safari (non-Gregorian calendar), Chrome (problem with
    <iframe>, IE (toFixed), Firefox (ISO date bug) - I'd have to mark it
    "best not viewed at all".

    Granted, it is commonly wrong to call for "Browser X" - but "anyone" is
    too strong. I have a part-page which is ONLY worth reading in Opera
    (except for authors of rival browsers, who may wish to gloat).

    Parts of your Web site still need that latter marking.

    --
    (c) John Stockton, nr London, UK. ???@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.

    Food expiry ambiguities: <URL:http://www.merlyn.demon.co.uk/date2k-3.htm#Food>
    Dr J R Stockton, Sep 6, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. barry
    Replies:
    4
    Views:
    3,786
    barry
    Apr 3, 2005
  2. Felix Piskunov

    Problem with setInterval(callserver,[period])

    Felix Piskunov, Nov 5, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    444
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Nov 6, 2006
  3. Daniel

    Using setInterval inside an object

    Daniel, Jun 30, 2003, in forum: Javascript
    Replies:
    28
    Views:
    403
    Daniel
    Jul 5, 2003
  4. Robert Mark Bram

    SetTimeout and SetInterval - since when?

    Robert Mark Bram, Jul 10, 2003, in forum: Javascript
    Replies:
    1
    Views:
    121
    Lasse Reichstein Nielsen
    Jul 10, 2003
  5. Richard A. DeVenezia

    IE6: setInterval, this==window arrgh

    Richard A. DeVenezia, Sep 7, 2003, in forum: Javascript
    Replies:
    10
    Views:
    260
    Lasse Reichstein Nielsen
    Sep 8, 2003
Loading...

Share This Page