tags:

views:

29

answers:

3

Hi All,

I have a method which calls a another method which inturns has some ajax call to the back end.

MethodB(){
MethodA()// this has some ajax functionality 
setpopupposition()// wants to overwrite x,y postions after MethodA sets...X,y postion.
}

MetodhA(){
AjaxCall();
Setx Popup to x,y location
}

The problem here is MethodA sets the popup postion to a x,y location ,but i want to overwrite x,y location to a different postion after the call to MethodA.The issue i am facing here is my setpoupuppostion in MethodB is getting executed before MethodA.

I want to execute setupuppostion() inside MEthodB only after methodA completes, can you please advice me how to do that.

+1  A: 

MethodA is completing, though. All you have in Method A is the AJAX call and the popup position setting function. These will both get called and then Method B will move on. (Note Method A is not waiting for the AJAX call to complete. It's just initiating it.)

An alternative would be to have a callback generated by the AJAX call, true on success/false on failure, and then pass that on from Method A to Method B. In Method B you could implicitly wait for that return before continuing, or only continue if it's true.

function MethodB() {
    if (MethodA()) {
       setPopupPosition();
    }
}

function MethodA() {
    if (AjaxCall()) { // You need to edit your AjaxCall() to return true/false
        Setx Popup to x,y location
        return true;
    }
    return false;
}

If you need help making your AJAX call return true/false, update the question with your existing AJAX method and I'll update it for you.

Robert
A: 

This is because your trying to run synchronously which is not a good idea.

what you want to do is set a callback like so

function MethodA()
{
    AjaxCall(function(){
        //SET X / Y
    });
}

function AjaxCall(callback)
{
    $.post('blah',{data:x},function(){
        //now the data has returned

        callback(); //Executes the function defined in MethodA();

    });
}
RobertPitt
A: 

Thanks for your answers. The problem for me is , i am doing something on the existing code.Method A uses old way of making ajax calls through xmlhttprequest and that method is there in someother javasscript file.Is there any other simple way to execute my code after that method call gets completed. I may not be able to post the complete code as its there in several files. thanks once again for your time.

gov