views:

41

answers:

4

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

if ( $.ajax.inProgress ){ do this; } else { do that; }
+2  A: 

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.

Sarfraz
Sometimes best solutions are the simplest ones :) Thank you!
bah
@bah: You are welcome and agreed :)
Sarfraz
+1  A: 

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
+2  A: 

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
A: 

You might like this:

    $("#contentLoading").ajaxSend(function(r, s) {
        $(this).show();
        $("#ready").hide();
    });

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