Best implementation of setTimeout / clearTimeout
- From: bizt <bissatch@xxxxxxxxxxx>
- Date: Wed, 1 Oct 2008 02:20:13 -0700 (PDT)
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
.
- Follow-Ups:
- Re: Best implementation of setTimeout / clearTimeout
- From: Thomas 'PointedEars' Lahn
- Re: Best implementation of setTimeout / clearTimeout
- Prev by Date: Toggling the SurroundContents method
- Next by Date: Re: Partial evaluation in JavaScript
- Previous by thread: Toggling the SurroundContents method
- Next by thread: Re: Best implementation of setTimeout / clearTimeout
- Index(es):
Relevant Pages
|