0

I have a for loop which invokes a function with loop variable as the paramter. i need the for loop to wait until the first invocation is executed, before the next

for(i=1;i<6;i++){
 demo($firstFrame,i)
}

how do we make the for loop wait.

The demo function has a timeout in it there for obviously the for loop will have to wait before invoking the function for the second time.

4
  • 3
    You can use a callback function Commented Aug 5, 2015 at 5:46
  • could you elaborate..? Commented Aug 5, 2015 at 5:48
  • Pls check this link javascriptissexy.com/… Commented Aug 5, 2015 at 5:49
  • 1
    stackoverflow.com/questions/8027392/… Does this help you? I suggest you to write your logic behind posting only single for-loop here. Some expert of stackoverflow may have better way to solve this problem instated of waiting. Commented Aug 5, 2015 at 5:51

2 Answers 2

2
     var demo = function(name, index, onCompleteFunc){
         /**do staff**/
         onCompleteFunc();// exec this func when you are done.
     }; 

     var iterator = function(iteration){
         if(iteration >= 0){
             demo($firstFrame, iteration, function(){
                        iterator(iteration-1);
             });
         }  
     }

     iterator(5);//this function start recurcively execute demo with indexes from 5 to 0
Sign up to request clarification or add additional context in comments.

Comments

1

use recurcive function;

demo($firstFrame,1)

function demo(firstFrame,i){
    if(i<6){
       //do something
       i++;
       setTimeout(function(){
          demo(firstFrame,i);
       },1000)
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.