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. Advertisements

  2. But which IE?
    Syntactically it is fine.
    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.
    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.
    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. Advertisements

  3. But which IE?
    Syntactically it is fine.
    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.
    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.
    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
    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.
     
    Dr J R Stockton, Sep 3, 2011
    #4
  5. Tom de Neef

    Tom de Neef Guest

    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.
    Always a pleasure to read your formal corrections. I learn from it but I
    fear I'm too old to catch up.
    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. Yes, indeed.
    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
     
    Thomas 'PointedEars' Lahn, Sep 5, 2011
    #6
  7. In comp.lang.javascript message <>,
    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.
     
    Dr J R Stockton, Sep 6, 2011
    #7
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.