Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    2 overlaped loops of setTimeout

    hello,
    So, there is a main loop with a setTimeout inside - it's made to move some DIVs the one after the other :
    Code:
     function Mainfunc() {
       for (i = 0; i < top.parent.gauche.stageId[num_div].length ; i++) {
           ....
           if (i> 0) 
                timerMain=setTimeout('goDown('+num_div+', '+i+')', 1200) ;
           else 
                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 ....
    Code:
    function goDown((num_div, i) {
        .....
        Perform_Anim()
    }
    Code:
    Perform_Anim() {
    	if(timer) clearTimeout(timer) ;
    	moveAreaAny(speed1,speed2)
    }
    Code:
    function moveAreaAny(moveH,moveV){
    	....	
    // 	This is in a loop :
    //           =========== 
    	this.moveArea(x,y)
    	timer=setTimeout(this.obj+".any("+moveH+","+moveV+")",retard)
    }
    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
    Ben

  • #2
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 :

    Code:
    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)
        else
            clearTimeout(timer) ;
    }
    I wonder if that is not the kind of things called 'recursive' programm ...

    Have a good sunday
    Ben


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •