正在进行Ajax请求的jQuery

bah*_*bah 12 javascript ajax jquery

有没有办法检查是否有正在进行的ajax请求?就像是:

if ( $.ajax.inProgress ){ do this; } else { do that; }
Run Code Online (Sandbox Code Playgroud)

Gau*_*rma 11

就在这里


$.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')
     }
});


当ajax请求刚刚启动时 ,beforeSend将执行您需要执行的过程,并且当ajax请求完成时将调用complete.
文件


Sar*_*raz 7

你基本上应该在脚本集的基础上设置一个变量,false并在ajax init上设置它true并在success处理程序中将其设置false为如此处所述.

  • 有时最好的解决方案是最简单的:)谢谢! (3认同)

Sta*_*ing 5

要中止ajax请求,请使用

if($.active > 0){ 
ajx.abort();//where ajx is ajax variable
}
Run Code Online (Sandbox Code Playgroud)

要继续运行ajax请求,请使用

if($.active > 0){ 
return;

} 
Run Code Online (Sandbox Code Playgroud)