0

I have 2 scripts

script1 = function() {
    // takes very long time
}

script2 = function() {
    // takes less time
}

I want to ensure that script2 should run after script1 is complete.

1
  • 1
    So is there a problem with calling script2() from the end of script1()? Commented Feb 29, 2012 at 5:50

2 Answers 2

3

put your script2 to script1. Or for more general solution make script1 function to receive callback function(script2) and call it.

This is the way of many many jquery async functions

script1 = function(callback){
      //takes very long time
      callback();
}


script2 = function(){
      //takes less time
}
...
//call like this
script1(script2);
Sign up to request clarification or add additional context in comments.

Comments

0

what I did before is to set a global bool variable and only assign it to true in the end in script1, and in script2, check the variable, if it's not true, use setTimeOut to call itself later. I know it sounds not a good solution but that's what I realized I can do.

2 Comments

Although, this may work, it's generally considered poor practice to poll like this when the traditional callback is adequate in this case.
thanks for the reply yes i did the same simon but i want to is there any other work around

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.