473,467 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

setTimeOut and infinite recursion

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

Sep 16 '06 #1
9 6696
pe**********@gmail.com wrote:
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

There are a limited number of timers , so
if you use them all up , setTimeout() and setInterval() would
return null in lue of timer id's and do nothing.

If I intend to have a lot of "waiting" , I typically
use a single setInterval() function to pump a routine
that manages a timestamped collection of targets

This is what I do to support retries on multiple
concurrent transactions from javascript to my server backend
and beyond.
Sep 16 '06 #2
drclue <dr****@drclue.netwrote in news:ym0Pg.12585$bM.8435
@newsread4.news.pas.earthlink.net:
I typically
use a single setInterval() function to pump a routine
that manages a timestamped collection of targets
Interesting. Can you give us an example, or point to a page?
Sep 17 '06 #3
Hi,

Lee wrote:

<snip>
By the way, to "beg the question" doesn't mean what you think it means:
http://begthequestion.info/
So now it's official, there really is a webpage about *everything* ;-)

That was instructive!

Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 17 '06 #4
Jim Land wrote:
drclue <dr****@drclue.netwrote in news:ym0Pg.12585$bM.8435
@newsread4.news.pas.earthlink.net:
>I typically
use a single setInterval() function to pump a routine
that manages a timestamped collection of targets

Interesting. Can you give us an example, or point to a page?
Heres a simplified description with some pseudo code.

Basically you for each wait type event
create an object containing all the information to describe the time
period and the desired action in a self contained way.
perhaps like this.
var aMyWait=[]
var oMyWait={start:new Date().valueOf(),wait:700,szEval:"alert('hi')"}

Now take that oMyWait object, and stick it in an array

aMyWait[aMyWait.length]=oMyWait

now have a setInterval pumped function iterate
the array checking each oMyWait type object in your aMyWait array
and when the time has elapsed eval the expression and remove the
object from the array.

function throbber()
{
var x=0
var tTimeNow =new Date().valueOf();
// Scan for stale connections
for(x=0;x<aMyWait.length;x++)
{
var thisWait=aMyWait[x]
if(tTimeNow-thisWait.start>thisWait.start+thisWait.wait)
{
eval(thisWait.szEval)
aMyWait.splice(x,1)
}
}
}




Sep 17 '06 #5
drclue wrote:
[...]
There are a limited number of timers , so
if you use them all up , setTimeout() and setInterval() would
return null in lue of timer id's and do nothing.
What is the limit that you have discovered? There's no limit noted in
either Gecko or MSDN documentation. I tested setting concurrent
timeouts[1] in Firefox and IE - Firefox was happy up to 100,000[2] (and
probably beyond, my patience ran out), IE seems to get lost somewhere
between 60,000 and 70,000. You can set a much higher number
consecutively without any problems - both browsers were happy to set
500,000 consecutive timeouts in blocks of 10,000 set concurrently.

Firefox seems to allocate timer numbers sequentially from 1, IE uses
some other scheme that numbers timeouts around 2.7e7. There is a
simple test below - it will consume quite a bit CPU when running. You
can watch the memory use climb as more timers are set, then see it fall
as they are executed.

If you are setting in excess of say 10,000 timers concurrently there is
something seriously wrong with your application architecture (which is
probably true if you are setting more than about 10 concurrently). And
if you are setting more than 500,000 consecutively, you're probably
expecting to set one every few seconds over several weeks in which case
you need to look seriously at memory management (and whether JavaScript
is the right tool for the job).
<script type="text/javascript">

function emptyFn() {};

var lotsTimer, numRuns = 0;

function setLotsOfTimers( limit ){
++numRuns;
setTimers(limit);
lotsTimer = setTimeout('setLotsOfTimers(' + limit + ');',20000);
}

function setTimers( limit ){
msgString = 'Reached limit: ' + limit;
var msgEl = document.getElementById('xx');
while ( limit-- ){
var x = setTimeout('emptyFn();', 100);
if ('number' != typeof x){
msgEl.innerHTML = 'Didn\'t finish.<br>Last setTimeout: '
+ x + '<br>Runs: ' + numRuns;
if (lotsTimer) clearTimeout(lotsTimer);
return;
}
}
msgEl.innerHTML = msgString + '<br>Last setTimeout: '
+ x + '<br>Runs: ' + numRuns;
}
</script>
<form action="">
<input type="text" name="limit" value="10000">
<input type="button" value="Set timers"
onclick="setTimers( this.form.limit.value );">
<input type="button" value="Set lots..."
onclick="setLotsOfTimers(this.form.limit.value);">
<input type="button" value="Cancel lots"
onclick="if (lotsTimer){clearTimeout(lotsTimer);lotsTimer=null ;">
</form>
<div id="xx"></div>

1. "Concurrent" meaning all set within a loop so that all of them are
set before the first one runs

2. I think setting 100,000 concurrent timeouts in a javascript
application is absurd, it was done here purely to see if I could find a
limit. I think the practical limit is probably about 1,000 or less.
--
Rob

Sep 18 '06 #6
RobG wrote:
drclue wrote:
[...]
>There are a limited number of timers , so
if you use them all up , setTimeout() and setInterval() would
return null in lue of timer id's and do nothing.

2. I think setting 100,000 concurrent timeouts in a javascript
application is absurd, it was done here purely to see if I could find a
limit. I think the practical limit is probably about 1,000 or less.
Perhaps over time the limit has gone away, but at one time
I recall an 8 timer limit.

I think I'll keep with the setInterval and array approach
as it gives me a lot more bank for the buck, but none the less
it's a good thing to know that one can fill the browser with timers.
Thanks for the info!

Sep 18 '06 #7
JRS: In article <11**********************@i3g2000cwc.googlegroups. com>,
dated Sat, 16 Sep 2006 16:45:02 remote, seen in
news:comp.lang.javascript, pe**********@gmail.com posted :
>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.
There is no need for wait() or sleep(), as setTimeout does all that is
required.
>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?
Of course not. Each currently-waiting timer uses resource, that is all.
Will the users machine eventually run out of
memory space to contain this stack?
Therefore, no.
How can this be avoided?
It cannot be achieved in that fashion.
>-- Whit Nelson
Defective signature format.

--
© John Stockton, Surrey, UK. REPLYyyww merlyn demon co uk Turnpike 4 ©
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html-Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm: about usage of News.
No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
Sep 19 '06 #8
JRS: In article <4l*****************@newsread4.news.pas.earthlink. net>,
dated Sun, 17 Sep 2006 18:19:44 remote, seen in
news:comp.lang.javascript, drclue <dr****@drclue.netposted :
>Heres a simplified description with some pseudo code.

Basically you for each wait type event
create an object containing all the information to describe the time
period and the desired action in a self contained way.
perhaps like this.
var aMyWait=[]
var oMyWait={start:new Date().valueOf(),wait:700,szEval:"alert('hi')"}
That seems inefficient. A timeout does not need to know when it
started, nor how long it is; it only needs to know how much longer to
wait. Don't repeat work unnecessarily.
>Now take that oMyWait object, and stick it in an array

aMyWait[aMyWait.length]=oMyWait

now have a setInterval pumped function iterate
the array checking each oMyWait type object in your aMyWait array
That is inefficient. Maintain the array in timeout order, and then only
the leading elements need be checked. Use insertion sort, since that
only needs a partial scan of the array at each insertion. Typically,
too, a long timeout will be inserted rarely, so the average insertion
scan will be shortish. (There may be even better ways.)
>and when the time has elapsed eval the expression and remove the
object from the array.
There will be no need for eval if instead a function name is in the
Object - just call the function. Another object element can be used to
provide a structure of parameters to the function. That may well be
quicker, and should not be significantly slower.

One should only use eval when it is useful to do so.
Rather than using setInterval, ISTM that one should, before starting the
"current" event(s), set a timeout that will run until the next loaded
event (insertion of a short interval during a long one will need to re-
schedule the event that was being waited for).

It's a good idea to read the newsgroup and its FAQ.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 19 '06 #9
Did you call my signature defective? I think your FACE is defective.

Sweet burn.

--------- RALPH waldO EM3rRson! +++++++

Dr John Stockton wrote:
JRS: In article <11**********************@i3g2000cwc.googlegroups. com>,
dated Sat, 16 Sep 2006 16:45:02 remote, seen in
news:comp.lang.javascript, pe**********@gmail.com posted :
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.

There is no need for wait() or sleep(), as setTimeout does all that is
required.
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?

Of course not. Each currently-waiting timer uses resource, that is all.
Will the users machine eventually run out of
memory space to contain this stack?

Therefore, no.
How can this be avoided?

It cannot be achieved in that fashion.
-- Whit Nelson

Defective signature format.

--
© John Stockton, Surrey, UK. REPLYyyww merlyn demon co uk Turnpike4 ©
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html-Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm: about usage of News.
No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
Sep 24 '06 #10

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

Similar topics

10
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...
2
by: Gordan | last post by:
hi i want to have a "close" button that user can click but that will also "click itself after 10 sec" so heres what i did function auto_close(x) { if(x>0){ document.form.button.value="CLOSE...
18
by: Frances Del Rio | last post by:
this code is supposed to flip imgs at intervals of one second, but it flipps them much faster, it seems it flips them all in that first second, the more seconds the faster it flips them, instead of...
4
by: LOPEZ GARCIA DE LOMANA, ADRIAN | last post by:
Hi all, I have a question with some code I'm writting: def main(): if option == 1: function_a()
4
by: Gregory Piñero | last post by:
Hi, Would anyone be able to tell me why my function below is getting stuck in infinite recusion? Maybe I'm just tired and missing something obvious? def...
0
by: Nays | last post by:
Hi All I would be very grateful for any help with a problem I am having with Custom Cultures. I need to change the date format of a specific culture. I have created a custom culture that...
4
by: Travis | last post by:
I"m looking to find (or if I can't, make) a real simple tree with a few characteristics - no sorting, I want to insert a node and specify the parent - infinite children These characteristics...
4
by: si4 | last post by:
Hello, I'm currently studying javascript by myself. I thought that I will write js that will remove element in the body from DOM every 2 seconds, untill all elements in the body are deleted(...
5
by: Tinku | last post by:
I am sorry for asking this silly question, but i don't understand why it is happening please suggest me ================================= #include <stdio.h> int main() { static int i=1;...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
1
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.