Javascript Ajax 请求正在进行 jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2872287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax request in progress jQuery
提问by bah
Is there a way to check if there's ajax request in progress? Something like:
有没有办法检查是否有正在进行的ajax请求?就像是:
if ( $.ajax.inProgress ){ do this; } else { do that; }
采纳答案by Sarfraz
You should basically set a variable on top of script set to falseand on ajax initiate set it to trueand in the successhandler set it to falseagain as described here.
您基本上应该在脚本集之上设置一个变量,false并在 ajax 启动时将其设置为true并在success处理程序中将其设置为false再次如此处所述。
回答by Gaurav Sharma
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 beforeSendwill do the process you need to do when the ajax request has just started and completewill be called when the ajax request is complete.
documentation
该beforeSend将你需要的时候Ajax请求刚刚开始,做的过程中完成的AJAX请求完成时将被调用。
文件
回答by StartCoding
To abort ajax request, Use
要中止 ajax 请求,请使用
if($.active > 0){
ajx.abort();//where ajx is ajax variable
}
To continue running ajax request, Use
要继续运行 ajax 请求,请使用
if($.active > 0){
return;
}
回答by Gutzofter
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);
});
});
});
回答by kgiannakakis
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.
如果您完全控制 javascript 代码,则可以在启动 ajax 请求时增加变量,并在请求完成(成功、失败或超时)时减少变量。通过这种方式,您始终知道是否有正在进行的请求。

