Skip to main content
Inspiring
November 1, 2020
Answered

How to add delay in for loop

  • November 1, 2020
  • 1 reply
  • 775 views

Hi,

I want to delay in for loop. How can i do? 

I will be grateful if you could help me.

 

Good work.

    This topic has been closed for replies.
    Correct answer ClayUUID

    Code-driven animation is based on timers. Your animation setup code will set up all the required variables, then call a function that performs the first step of the animation. This function will then perform the first step of the animation, and if there are more steps, use a setTimeout() call to schedule itself to run again after the desired delay. This will repeat until there are no more steps.

    1 reply

    Legend
    November 1, 2020

    You DON'T put a delay in a for loop. What are you actually trying to do that you think this is a good idea?

    Ece5F99Author
    Inspiring
    November 1, 2020

    import flash.display.MovieClip;
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    import flash.utils.setTimeout;

    var bh:Symbol1 = new Symbol1();
    var bhs:Symbol2 = new Symbol2();
    var quest:Array = [1,0,0,0,1,1,1,0,0,0,1,1,1,1,0,0,0,1,0,1];
    var len:uint = quest.length;

     

    function bhchild(){
    bh = new Symbol1();
    addChild(bh);
    bh.x = 415.7;
    bh.y = 149.6;
    }

    function bhschild(){
    bhs = new Symbol2();
    addChild(bhs);
    bhs.x = 519.25;
    bhs.y = 149.6;
    }

    for(var i:uint = 0; i < len; i++)
    {

    if(quest[i] == 0)
    {
    bhchild();
    //setInterval(bhchild, 1000);
    }
    else
    {
    //setInterval(bhschild, 1205);
    //setTimeout(bhschild, 100);

    bhschild();
    }
    }

    stop();

     

    According to the example above, I want the for loop to play the MovieClip type symbols 20 times. When I check with Debug my loop runs as many times as I want. However, when I run the animation, I cannot play the animation in the order the code is running. This animation is only once work.