8

I have the following code:

myFunc();
bar();

myFunc() is making an ajax request

I don't want to execute bar() until myFunc()'s request has completed.

I also do not want to move the call to bar() inside of myFunc.

possible?

EDIT

Here is the code I ended up with:

var FOO = {
 init: function(blah)
 {
  // Callbacks to pass to the AJAX challenge data load
  var callbacks = {
   myFunc1: function(){ myFunc1(blah); },
   myFunc2: function(){ myFunc2(blah); },
  };

  this.bar(callbacks); // Load the challenge data and setup the game
 },
 bar: function(callbacks) { ..loop through and execute them.. }

};
3
  • 1
    Jan and Jerome both have valid answers, personally I'm leaning towards Jeromes since thats the typical way to do stuff in JavaScript Commented Sep 2, 2010 at 8:36
  • Why are you reinventing the wheel? There's no justifiable reason to deal with all the cross-browser quirks instead of using a premade Ajax library that supports callbacks. Commented Sep 2, 2010 at 8:41
  • I wasn't trying to reinvent the wheel. I just didn't know what I was doing, period. Jerome's suggestion worked out great. See the EDIT for what I ended up with: I think that's ideal... Commented Sep 2, 2010 at 17:37

4 Answers 4

6

In order to do what you are looking for, you must find a way for bar() to communicate with myFunc().

When you say that you do not want to move the call to bar() inside myFunc() this can be interpreted in several ways.

For example, you could make bar() a parameter of myFunc()

function bar() {
   // do what bar() does
}

function myFunc(callback) {
   // use callback in the onreadystatechange property of the xmlhtprequest object
}

myFunc(bar)

I hope this will help you

Jerome Wagner

Sign up to request clarification or add additional context in comments.

Comments

1

IF you can add a kind of a flag that will indicate that myFunc() has finished with its request you can do it, but this is not a good idea - best practice is to use callbacks.

Try this code:

var successRequest = false;
myFunc(); // when get the response set successRequest to true

var interval = setInterval(function() {
    if (successRequest) {
        clearInterval(interval);
        bar();
    }
}, 100);

1 Comment

Does setInterval works as global? I mean if I set interval at some function and if I reset it another(or at same function with a new rewuest) does it understands other setIntervals or not?
0

No, not possible unless you use a synchronous request which is no longer AJAX and something I wouldn't recommend you doing as it will freeze the UI until the request completes and users hate having their browser freezing.

Comments

0

No, it is not possible.

You could achieve it with the side effect that you lock up the browser by making a SJAX request instead of an AJAX request, but that is a terrible idea.

Learn to love event driven programming when you work in an event driven programming environment.

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.