473,867 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best implementation of setTimeout / clearTimeout


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 9542
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).
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

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 the picture to not be able to appear on the display, obviously. Here's the calling code (snipped...
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 I've tested with, except for one problem on every browser: the "clearing" of the menu.
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 NAME='ImageSrc' VALUE='Null'>")
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() {
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
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 of memory space to contain this stack? How can this be avoided? Thanks, -- Whit Nelson
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 odd about this example is that if I click the "Make Happiness
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 what happens after the timeout is finished. Here is my code: var changeBG; function...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.