0

I have something like this :

<html>
<script type="text/javascript">
    //BLOCK1
    function startingFunction() {
        //do a server call, receive the response through callback functions, either successFunction or failFunction

        onSuccess : successFunction,
        onFailure : failFunction
    }

    function successFunction(result) {
        //called receiving the server response
        //configure something based on success
    }

    function failFunction(result) {
        //called receiving the server response
        //configure something based on failure
    }
</script>

<script type="text/javascript">
    // BLOCK2
    //javascript code that needs to wait for the result of the configuration made above in BLOCK1 in order to continue doing more stuff
</script>

<body>...</body>
</html>

How can i make the javascript code in BLOCK2 to wait until the server has responded something and the configuration has been made?

8
  • Why not use the ol' trusty $( document ).ready(function() { ? There's a jQuery tag on this question after all Commented May 17, 2016 at 21:08
  • assuming jquery ? not a good assumption. Commented May 17, 2016 at 21:09
  • 1
    @Abhishek Not an assumption, question tagged as such Commented May 17, 2016 at 21:09
  • Do you mean from an AJAX request or when the page has fully loaded? Commented May 17, 2016 at 21:09
  • oops. i misspoke. Perhaps that tag should be removed. There is nothing specific to jquery in this question. Commented May 17, 2016 at 21:10

3 Answers 3

3

You can simply call a function in your second block from both functions that could be called in the first block. (assuming you want to continue after a failure)

<script type="text/javascript">
    //BLOCK1
    function startingFunction() {
        //do a server call, receive the response through callback functions, either successFunction or failFunction

        onSuccess : successFunction,
        onFailure : failFunction
    }

    function successFunction(result) {
        //called receiving the server response
        //configure something based on success
        afterConfiguration();
    }

    function failFunction(result) {
        //called receiving the server response
        //configure something based on failure
        afterConfiguration();
    }
</script>

<script type="text/javascript">
    // BLOCK2
    //javascript code that needs to wait for the result of the configuration made above in BLOCK1 in order to continue doing more stuff

    function afterConfiguration(){
      //code that needed to wait
    }

</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Just place the code in block two inside a function, then invoke that function at the end of successFunction.

Then again, I don't see the point of splitting this up in two script elements.

Comments

-1

You could also use a flag and check periodcally using setInterval():

Using a Flag:

<html>
<script type="text/javascript">
//BLOCK1
var block1Success = false; // <-- this is our flag
function startingFunction() {
    //do a server call, receive the response through callback functions, either successFunction or failFunction

    onSuccess : successFunction,
    onFailure : failFunction
}

function successFunction(result) {
    //called receiving the server response
    //configure something based on success
    block1Success = true; // <-- flag is set here
}

function failFunction(result) {
    //called receiving the server response
    //configure something based on failure
}
</script>

<script type="text/javascript">
// BLOCK2
//javascript code that needs to wait for the result of the configuration      made above in BLOCK1 in order to continue doing more stuff
window.setInterval(
    function(){
       if(block1Success){ 
         // code need to be executed here..
         alert("block1 success executed");
       }
    }, 100)
</script>

<body>...</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.