Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > setInterval

Reply
Thread Tools

setInterval

 
 
Tom de Neef
Guest
Posts: n/a
 
      09-02-2011
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


 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      09-02-2011
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.
 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      09-02-2011
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.
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      09-03-2011
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.
 
Reply With Quote
 
Tom de Neef
Guest
Posts: n/a
 
      09-05-2011
"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


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      09-05-2011
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
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      09-06-2011
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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with setInterval(callserver,[period]) Felix Piskunov ASP .Net 1 11-06-2006 02:10 AM
Window.setInterval not working from asp.net barry ASP .Net 4 04-03-2005 04:55 PM
IE6: setInterval, this==window arrgh Richard A. DeVenezia Javascript 10 09-08-2003 02:14 AM
SetTimeout and SetInterval - since when? Robert Mark Bram Javascript 1 07-10-2003 02:15 AM
Using setInterval inside an object Daniel Javascript 28 07-05-2003 03:05 PM



Advertisments