Click to See Complete Forum and Search --> : Multiple setTimeouts ?


gazolba
12-09-2002, 01:51 PM
Can I have multiple setTimeouts active at one time? I cannot find the answer to this in any of my Javascript books.
But when I try it, I get strange results that lead me to believe my clearTimeouts are not working.

e.g. I set up a loop using setTimeout with an interval of 5000 millisecs, within this loop I set up 2 consecutive setTimeouts of 20 each, clearing each after they finish. Both of the inner timeouts are recursive and execute 50 times before terminating (i.e. a total of 1000 millisecs each).

What is happening is that after the first of the 'inner' timeouts finishes and I have cleared it, my second 'inner' timeout begins but is then immediately cut short by the first inner timeout firing again. I use different timeout names for all 3 timeouts.

Is there any way to eliminate the timouts and use a programmed delay (a wait) instead?

Does setInterval work with the latest browsers, I have not been able to get it to work with IE 6?

gil davis
12-09-2002, 04:27 PM
Originally posted by gazolba
Can I have multiple setTimeouts active at one time?
Yes, provided they each have a unique pointer assigned for clearing it as necessary, and do not use any global variables in a conflicting way.
I get strange results that lead me to believe my clearTimeouts are not working.
Without the code, you won't get much specific help.
I set up 2 consecutive setTimeouts of 20 each, clearing each after they finish./quote]
You don't have to clear a "setTimeout()" if it has finished. You only clear a "setTimeout()" if you want to prevent it from finishing and calling the function.
[quote]Both of the inner timeouts are recursive
Are they reentrant (see global variables hint above)?
Is there any way to eliminate the timouts and use a programmed delay (a wait) instead?
No. There is nothing in JS that just wastes time. You have to have some kind of event to trigger things.
Does setInterval work with the latest browsers
The function "setInterval()" has been available in browsers starting in JavaScript 1.2 (NS 4, IE 4)
I have not been able to get it to work with IE 6?
Maybe you have a common error, since you cannot get "setTimeout()" to work correctly, either.

Here is a working example for setInterval() (tested in NS4.8 and IE 5.5):
<script language="Javascript1.2" type="text/javascript">
<!--
var tags_before_clock = "<small>"
var tags_after_clock = "</small>"
if (document.layers)
{document.write('<layer id="clock"></layer><br>');}
else
{document.write('<span id="clock"></span><br>');}
function upclock(){
var dte = new Date();
var hrs = dte.getHours();
var min = dte.getMinutes();
var sec = dte.getSeconds();
var col = ":";
var spc = " ";
var apm;
if (12 < hrs)
{apm="PM";
hrs-=12;}
else
{apm="AM";}
if (hrs == 0) hrs=12;
if (min<=9) min="0"+min;
if (sec<=9) sec="0"+sec;
var clk = tags_before_clock + hrs + col + min + col + sec + spc + apm + tags_after_clock;
if (document.layers)
{document.clock.document.write(clk);
document.clock.document.close();}
else
{if (document.getElementById)
{document.getElementById("clock").innerHTML = clk;}
else
{document.all["clock"].innerHTML = clk;}
}
}

setInterval("upclock()",1000);
//-->
</script>