setInterval

T

Tom de Neef

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
 
R

Richard Cornford

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

Richard Cornford

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

Dr J R Stockton

In comp.lang.javascript message <[email protected]
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.
 
T

Tom de Neef

Thomas 'PointedEars' Lahn said:
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().


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

Thomas 'PointedEars' Lahn

Tom said:
"Thomas 'PointedEars' Lahn" [wrote:]
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
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top