View Full Version : 2 overlaped loops of setTimeout

04-16-2005, 09:32 PM
So, there is a main loop with a setTimeout inside - it's made to move some DIVs the one after the other :

function Mainfunc() {
for (i = 0; i < top.parent.gauche.stageId[num_div].length ; i++) {
if (i> 0)
timerMain=setTimeout('goDown('+num_div+', '+i+')', 1200) ;
goDown(num_div, i) ;
as you understand, I don't need to wait the very first time, then I need to wait because inside the goDown function, I call an other function with a loop and a setTimeout() inside ;
The goDown() function is made to move one DIV from one point to an other ....

function goDown((num_div, i) {

Perform_Anim() {
if(timer) clearTimeout(timer) ;

function moveAreaAny(moveH,moveV){
// This is in a loop :
// ===========
I hope,this description is not too fine but that's how it works, basically

So, the 2nd loop of setTimeout is OK ... it takes around 1 second to perform this loop and this is the reason why I wait 1.2 seconds in the first setTimeout before calling the function goDown the 2nd time ...
what happens :
_ one DIV to move = one call of the goDown() function => OK
_ 2 DIVs to move : one call of the goDown() function + wait 1.2 second and then the second call of goDown() is OK too ...
_ more than 2 DIVS to move => big mess ! I've tried to clearTimeout(timerMain) at the end of the goDown() function but that's stop completely the programm ... How can I do ? maybe in giving different names to the mainTimer ? but how ? why do the 2nd loop work and not the first ?

Thank you by advance for any help

04-17-2005, 02:36 PM
Hello again,

Well, I've been a little beat too brief yesterday when I have been writting // this is in a loop ... ! because the problem just standed about how this loops were written ... the 2 loops wrere not written on the same way, the one I have copied from an other script worked, the one I did on my own just didn't !!!
Now both of them work and I imagine we can overlap as much as loops as we want with different setTimeout ...

There's only one way to control a loop of setTimeout() with a controlled way out which stands on :

function myfunction (param1, param2)
// The processing will modify param1 and/or param2
// And can call an other function with an other setTimeout()
if (myCondition_for_the_loop_is_true)
timer = set timeout('myfunction('+param1+', '+param2')', timedelay)
clearTimeout(timer) ;
I wonder if that is not the kind of things called 'recursive' programm ...

Have a good sunday