Best implementation of setTimeout / clearTimeout



Hi,

I have a process where every so often I refresh a table's data using a
setTimeout() .. and when its no longer needed, a clearTimeout().

The following is a simple example of how this is done:

var goMenuTimeout = null;
function LoadMenu() {
// cancel any repeat actions
clearTimeout(goMenuTimeout);

// do some loading stuff
.
.
.

// set timeout to refresh in 60 seconds
goMenuTimeout = setTimeout("LoadMenu()", 60000);
}

This is just one example however my app has many setTimeouts to
refresh various elements. Now, the problem I may have is memory leaks
and this is one area I reacon it may be happening where objects
created with setTimeout are not being cancelled properly with
clearTimeout() and over time they are being retained in memory. Could
this be the case even tho I am using clearTimeout()?

I have also seen the following implementation:

clearTimeout(timeoutID);
delete timeoutID;

This is how I have seen this process done on the MDC and some other
sites, will this reduce memory usage? As far as I was aware
clearTimeout() would destroy the repeating object in the variable,
does it still get retained in memory? I have noticed that when I do
alert(goMenuTimeout) each time it gives me a different timeoutID value
each time so I suspicious that perhaps these timeout objects are
getting retained and over time memory is being allocated to redundant
objects. Could anyone shed some light on this matter as Ive tried
running some tests using Windows Task Manager to track memory usage
but some times the results can be a little inconclusive as I dont
really know if other factors of the app are having some effect also.
So long as I know Im know my implementation of setTimeout /
clearTimeout is correct I can rule that out as a cause.

Btw when I talk about memory leaks I meaning when I first load up
Firefox, in Windows Task Manager it is using 32KB of memory, but as
time goes on (ie. 20/30 minutes) it can be as high as 80/100KB with
only one tab open - in that time the app may have done one or two
hundred timeout processes. I have noticed once when the browser has
been open for a few hours that usage has risen to 400+KB which seems
quite extreme

Thanks

Burnsy
.



Relevant Pages

  • Re: Best implementation of setTimeout / clearTimeout
    ... the problem I may have is memory leaks and this is ... setTimeout are not being cancelled properly with clearTimeoutand over ... timeout objects are getting retained and over time memory is being ... There are other factors to consider, though: browser version (2.0.x leaked ...
    (comp.lang.javascript)
  • Re: Why do my Desktop Icons refresh slowly?
    ... If the card has its own memory, it's not sharing with main system memory. ... >>> from the "refresh rate" as applied to a monitor and graphics card. ... >>> rate applies to the speed at which the monitor refreshes the screen. ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Why do my Desktop Icons refresh slowly?
    ... If the card has its own memory, it's not sharing with main system memory. ... >>> from the "refresh rate" as applied to a monitor and graphics card. ... >>> rate applies to the speed at which the monitor refreshes the screen. ...
    (microsoft.public.windowsxp.basics)
  • Re: Why do my Desktop Icons refresh slowly?
    ... If the card has its own memory, it's not sharing with main system memory. ... >>> from the "refresh rate" as applied to a monitor and graphics card. ... >>> rate applies to the speed at which the monitor refreshes the screen. ...
    (microsoft.public.windowsxp.perform_maintain)
  • Re: Memory leaks
    ... to get the data form the ... Ajax except there is no external file called, the server interprets ... 'refresh page' or 'F5' the code runs but slower. ... unwise to invoke this kind of memory requirements. ...
    (comp.lang.javascript)