javascript - How to call a function after completion of all requests - Stack Overflow

I have two AJAX requests one after another- var firstResult;var secondResult;FirstAjaxrequest({callbac

I have two AJAX requests one after another-

var firstResult;
var secondResult;

FirstAjaxrequest({
        callback: function (options, success, response) {
            if(success)
            {
                firstResult = true;
            }
            else
            {
                firstResult = false;
            }
        }    
    });

SecondAjaxrequest({
     callback: function (options, success, response) {
            if(success)
            {
                secondResult= true;
            }
            else
            {
                secondResult= false;
            }
     }    
});

DisplayMessage(); // This function should display a message depending on     
                     FirstResult and secondResult

I have two AJAX requests one after another-

var firstResult;
var secondResult;

FirstAjaxrequest({
        callback: function (options, success, response) {
            if(success)
            {
                firstResult = true;
            }
            else
            {
                firstResult = false;
            }
        }    
    });

SecondAjaxrequest({
     callback: function (options, success, response) {
            if(success)
            {
                secondResult= true;
            }
            else
            {
                secondResult= false;
            }
     }    
});

DisplayMessage(); // This function should display a message depending on     
                     FirstResult and secondResult
Share Improve this question asked Jan 21, 2013 at 14:08 Microsoft DNMicrosoft DN 10k10 gold badges54 silver badges73 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 12

You could create a variable for each request, signifying a successful request. Change this variable for each request respectively, when a successful request is made, then call your function.

Within your function simply check both variables, if they're both true, then both requests were successful, otherwise at least one was not and the if() condition will be false.

var firstResult = false,
    secondResult = false;

FirstAjaxrequest({
    callback: function (options, success, response) {
        if(success) {
            firstResult = true;
            DisplayMessage();
        }
    }    
});

SecondAjaxrequest({
    callback: function (options, success, response) {
        if(success) {
            secondResult= true;
            DisplayMessage();
        }
    }    
});

function DisplayMessage(){
    if(firstResult && secondResult){
        //Both requests successful
    } else {
        //Either still waiting for a request to plete
        // or a request was not successful.
    }
}

There might be a better way to do it in EXTJS, but a plain JS solution is

var firstResult;
var secondResult;
var reqCount = 2;
var returnedCount = 0;

function isDone () {
    returnedCount++
    if ( returnedCount === reqCount ) {
         console.log(firstResult, secondResult);
    }
}

FirstAjaxrequest({
        callback: function (options, success, response) {
            if(success)
            {
                firstResult = true;
            }
            else
            {
                firstResult = false;
            }
            isDone();
        }    
    });

SecondAjaxrequest({
     callback: function (options, success, response) {
            if(success)
            {
                secondResult= true;
            }
            else
            {
                secondResult= false;
            }
            isDone();
     }    
});

Give this a try

FirstAjaxrequest({
            callback: function (options, success, response) {
                if(success)
                {
                    firstResult = true;
                }
                else
                {
                    firstResult = false;
                }
                SecondAjaxrequest({
                     callback: function (options, success, response) {
                            if(success)
                            {
                                secondResult= true;
                            }
                            else
                            {
                                secondResult= false;
                            }
                            DisplayMessage(); // Now we have access to first and second result as the 
                                             //as the second request is within the scope of the first request
                     },
                     scope: this // We can pass the first request to the second request as the context 
                });
            }    
        });

After the pletion of each request, you should check if the result of the other one is received or not. Some thing like this:

var result1 = null;
var result2 = null;
function DoAjax() {
    $.get(url1, {}, function (data) {
         result1 = data;
         DoSth();
    });
    $.get(url2, {}, function (data) {
         result2 = data;
         DoSth();
    });
}
function DoSth() {
    if(result1 != null && result2 != null) {
         //...
    }
}

Warning: jQuery answer. I'm not sure if extjs has similar support for Deferreds and Promises.

This is a perfect opportunity to learn about jQuery Deferred:

$.when(firstAjaxrequest(...), secondAjaxrequest(...))
.then(function (firstResult, secondResult) {
    ...
});

This assumes your firstAjaxrequest and secondAjaxrequest functions both return Deferred or Promise objects, such as those returned by $.ajax.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743634256a4481883.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信