473,569 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(go MenuTimeout);

// do some loading stuff
Oct 1 '08 #1
1 9517
bizt wrote:
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(go MenuTimeout);

// do some loading stuff . . .

// set timeout to refresh in 60 seconds goMenuTimeout =
setTimeout("Loa dMenu()", 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()?
Yes, objects created in code evaluated through window.setTimeo ut() are not
automatically garbage-collected when window.clearTim eout() is called.
I have also seen the following implementation:

clearTimeout(ti meoutID); delete timeoutID;

This is how I have seen this process done on the MDC and some other
sites, will this reduce memory usage?
If `timeoutID' was *not* declared a variable (and so the created property
would lack the DontDelete attribute), then the additional `delete' operation
would probably save about 12 bytes of memory (32 bits [4 bytes] for the
variable pointer and 64 bits [8 bytes] for the IEEE-754 double-precision
floating-point value that it points to).
As far as I was aware clearTimeout() would destroy the repeating object
in the variable,
The return value of window.setTimeo ut() and the argument that
window.clearTim eout() expects is a number value that is the index of the
timeout/interval in the internal timeout/interval registry of the host
environment, not a direct reference to an object.
does it still get retained in memory?
If the aforementioned index specifies an object to implement the code to be
executed on timeout/interval click, then that object will allocate memory
until it is garbage-collected.
I have noticed that when I do alert(goMenuTim eout) 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.
Although there is probably no timeout object as such, your suspicion is
warranted. The redundant objects would more likely to be the objects that
you create in the "do some loading stuff" part of the code instead.
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
Probably you mean 32 _MiB_ of memory. I have yet to see a Firefox version
with an initial memory footprint lower than 20 MiB.
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
There are other factors to consider, though: browser version (2.0.x leaked
much more memory than 3.0.x does), add-ons and plugins that leak memory, and
so on. Most important is that new browser windows do not create new browser
processes, and therefore contribute to the total memory footprint of the
single browser process (you were emphasizing that you have only one tab open
in the window in question).
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
Oct 1 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3046
by: Jupiter49 | last post by:
I'm trying to move pictures around on the screen, with a slight delay between the first picture and second picture (following onmousemove). When I add the setTimeout function I must be doing it wrong because the function that calculates the new coordinates for the picture returns 32 million and change instead of the coordinate. This causes...
1
11908
by: Mike | last post by:
Hey, I posted a question yesterday that was pretty confusing. I've figured out some of it for myself, but there's still one issue that I can't figure out, and I've been trying for months now! I have a pop-up menu; one of those where you scroll over the menu, and a submenu pops up beneath it. Everything's running smoothly on every browser...
29
8724
by: Mic | last post by:
Goal: delay execution of form submit Code (Javascript + JScript ASP): <% Response.Write("<OBJECT ID='IntraLaunch' STYLE='display : none' WIDTH=0 HEIGHT=0 CLASSID='CLSID:0AE533FE-B805-4FD6-8AE1-A619FBEE7A23' CODEBASE='IntraLaunch.CAB#version=5,0,0,3'>") Response.Write("<PARAM NAME='ImageLoc' VALUE='Null'>") Response.Write("<PARAM...
12
5525
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
17
2397
by: blueapricot416 | last post by:
This is a very basic question -- but I can't find the answer after looking for 20 minutes. If you code something like: function set_It() { setTimeout('Request_Complete("apple", -72)',5000) } and call it 50 times, will it cause problems because you are not
9
6712
by: pengypenguin | last post by:
As we know, Javascript suffers an insufferable lack of a wait() or sleep() function, and so setTimeOut must step in to take up the slack. A function can use setTimeOut like sleep() by calling itself after x seconds. This begs a few questions: Is this design building enormous calling stacks to nowhere? Will the users machine eventually run out...
4
2107
by: Rene | last post by:
Hi, first things first, please take a second to check out the page on the link below. http://bookofjavascript.com/Chapter13/Assignment.html In this page, you are supposed to click on the "Make Happiness Bounce" button to make the smiley face start bouncing and then you can click the "Stop that Smiley!" button to stop it. What I find...
3
5767
by: naurus | last post by:
i'd like to know what set timeout does after its finished. I have a script that starts a timeout right before sending an SJaX request that timesout after 1000 milliseconds and abort()s the SJaX request and sets some things then alerts me that it timed out. After i click ok on the alert, my browser (FF) crashes every time. I'd just like to know...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.