13

Is there a way to check if there's ajax request in progress? Something like:

if ( $.ajax.inProgress ){ do this; } else { do that; }
ManBehindTheCurtain
  • 2,498
  • 4
  • 24
  • 33

5 Answers5

13

yes there is


$.ajax({
     type: 'get',
     url: 'url',
     data: {
              email: $email.val()
     },
     dataType: 'text',
     success: function(data)
     {
        if(data == '1')
        {
            $response.attr('style', '')
                     .attr('style', "color:red;")
                     .html('Email already registered please enter a different email.');
        }
        else
        {
            $response.attr('style', '')
                     .attr('style', "color:green;")
                     .html('Available');
        }
     },
     beforeSend: function(){
                $email.addClass('show_loading_in_right')
     },
     complete: function(){
                $email.removeClass('show_loading_in_right')
     }
});


the beforeSend will do the process you need to do when the ajax request has just started and complete will be called when the ajax request is complete.
documentation

Gaurav Sharma
  • 2,830
  • 1
  • 37
  • 54
  • I don't think this addresses the OP's question. He doesn't want to know how to fire handlers prior to sending the request or after the request completes, he wants to fire certain events when a request is *ongoing*... – SexyBeast Feb 11 '13 at 10:08
8

To abort ajax request, Use

if($.active > 0){ 
ajx.abort();//where ajx is ajax variable
}

To continue running ajax request, Use

if($.active > 0){ 
return;

} 
StartCoding
  • 394
  • 5
  • 16
7

You should basically set a variable on top of script set to false and on ajax initiate set it to true and in the success handler set it to false again as described here.

Community
  • 1
  • 1
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • 3
    Sometimes best solutions are the simplest ones :) Thank you! – ManBehindTheCurtain May 20 '10 at 09:15
  • Hi! I am testing a webpage and I would like to know how will I get to know when the Ajax request is complete using a Java Script(i.e I have no control of the web page, I can just inject a Java script)? – Ishank Jul 31 '12 at 11:29
  • For simple pages, this is fine, but I have to say that an approach that demands that every developer on a project always remember to set a variable every time they do an ajax request AND update it both on failure and success (or on complete/finally) is not a good approach for any complicated project. – Vala Oct 05 '16 at 11:00
  • This approach fails as soon as multiple requests overlap. The first completing request will (incorrectly) set the variable to false. It's better to use $(document).ajaxStart() and $(document).ajaxStop(), see the answer of Gutzofter below for details. – Ole Jun 22 '17 at 17:42
4

You might like this:

$(document).ready(function() {
    var working = false;
    $("#contentLoading").ajaxSend(function(r, s) {
        $(this).show();
        $("#ready").hide();
        working = true;
    });

    $("#contentLoading").ajaxStop(function(r, s) {
        $(this).hide();
        $("#ready").show();
        working = false;
    });

    $('#form').submit(function() {
        if (working) return;
        $.post('/some/url', $(this).serialize(), function(data){
            alert(data);
        });
    });
});
Gutzofter
  • 2,003
  • 23
  • 26
2

If you are in full control of the javascript code you could increment a variable whenever you start an ajax request and decrement it when the request completes (with success, failure or timeout). This way you always know if there is a request in progress.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194