jqueryajaxajax-request

Detecting if specific jQuery.get() is in progress?


How do you detect if a specific ajax request is in progress, I can use done and fail to detect when it is finished and on fail, but is there a way to find if it is in progress?

var prog = false;
var ajxReq = $.get(url, function (response) {
    // insert response into container
})
.done (function () { prog = false; })
.fail (function () { });

var prog1 = false;
var ajxReq1 = $.get(url, function (response) {
    // insert response into container
})
.done (function () { prog1 = false; })
.fail (function () { });

I want to change prog variable value to true if ajxReq in progress and prog1 variable value to true if ajxReq1 in progress

Youssef


Solution

  • If you are looking for something for all your application's ajax then you can always use ajaxStart and ajaxStop for that:

    var isAjaxRunning = false;
    
    $(document).ajaxStart(function() {
         isAjaxRunning = true;
    });
    
    $(document).ajaxStop(function() {
          isAjaxRunning = false;
    });
    

    http://api.jquery.com/ajaxstart/
    http://api.jquery.com/ajaxstop/

    Otherwise simply do something like this:

    var prog = false;
    var ajxReq = $.get(url, function (response) {
        prog = true; 
        // insert response into container
    })
    .done (function () { prog = false; })