Php 如何停止ajax请求?
我有多个ajax请求,当其中一个请求无法获取我想要的数据时,我会重新发送它,直到它能够获取数据。 问题是在它得到数据后我无法阻止它。ajax中是否有中断或类似的中断? 我试过clearinterval,但没用 以下是我的功能:Php 如何停止ajax请求?,php,jquery,ajax,clearinterval,Php,Jquery,Ajax,Clearinterval,我有多个ajax请求,当其中一个请求无法获取我想要的数据时,我会重新发送它,直到它能够获取数据。 问题是在它得到数据后我无法阻止它。ajax中是否有中断或类似的中断? 我试过clearinterval,但没用 以下是我的功能: function ajaxGetServerDatabase(Div,val,interval){ console.log(val); dbs[val]=new Array(); $('#bck_action').val('
function ajaxGetServerDatabase(Div,val,interval){
console.log(val);
dbs[val]=new Array();
$('#bck_action').val('get_DB');
$('#server_ip').val(val);
post_data = $('#'+Div+' *').serialize();
$.ajax({
type: "POST",
url: document.URL,
data: post_data,
dataType: "text",
success: function(response) {
if (response!='no_connection'){
dbs[val]=JSON.parse(response)
clearInterval(this.interval); // ????
}
}
});
return false;
}
function ajaxGetDatabase(Div,ips,interval){
$.each(ips,function(i,val){
dbs[val]=new Array();
$('#bck_action').val('get_DB');
$('#server_ip').val(val);
post_data = $('#'+Div+' *').serialize();
// console.log(post_data);
$.ajax({
type: "POST",
url: document.URL,
data: post_data,
dataType: "text",
success: function(response) {
if (response!='no_connection'){
dbs[val]=JSON.parse(response)
}
else
{
setInterval("ajaxGetServerDatabase('"+Div+"','"+val+"','"+interval+"')", interval);
}
}
});
});
return false;
}
我称之为:
ajaxGetDatabase('tab_backup',ips,3000);
clearInterval
与ajax
无关。这只是一个计时器功能,其作用范围是通过setInterval
清除先前设置的计时器。如果您确实想使用计时器功能,则需要将变量附加到setInterval
函数,您可以通过clearInterval
设置将其清除为setInterval
中前面定义的id参数
var id = " ";
success: function(response) {
if (response!='no_connection'){
dbs[val]=JSON.parse(response)
clearInterval(id);
}
else
id= setInterval("ajaxGetServerDatabase('"+Div+"','"+val+"','"+interval+"')", interval);
}
或者您可以使用ajax中止代码
abort
或者类似的方法
...
var stopAjax = 0; //switch is on
if(stopAjax == 0){
$.ajax({
...
success: function(response) {
if (response!='no_connection'){
dbs[val]=JSON.parse(response);
stopAjax = 1; //switch is off
}
else{
setInterval("ajaxGetServerDatabase('"+Div+"','"+val+"','"+interval+"')", interval);
}
}
});
}
答案如下:
在ajaxGetDatabase中:
success: function(response) {
if (response!='no_connection'){
dbs[val]=JSON.parse(response)
}
else
{
id=setInterval("ajaxGetServerDatabase('"+Div+"','"+val+"')", interval);
}
success: function(response) {
if (response!='no_connection'){
dbs[val]=JSON.parse(response)
clearInterval(id);
}
}
在ajaxGetServerDatabase中:
success: function(response) {
if (response!='no_connection'){
dbs[val]=JSON.parse(response)
}
else
{
id=setInterval("ajaxGetServerDatabase('"+Div+"','"+val+"')", interval);
}
success: function(response) {
if (response!='no_connection'){
dbs[val]=JSON.parse(response)
clearInterval(id);
}
}
不带范围参数
var id;
为了使其通用化并适用于多个已停止的服务器(多个ajax请求失败),我使用了一个数组来保存ID,如下所示:
var ids=new Array();
ids[val]=setInterval("ajaxGetServerDatabase('"+Div+"','"+val+"')", interval);
clearInterval(ids[val]);
您是否正在查找
超时设置?乙二醇
$.ajax({
timeout: 10000,
...
});
XHR有一个abort()
函数,但一旦发送,它就被发送了。我的页面中还有其他ajax请求,如果我使用它,它们会被发送还是会杀死ajax?你可以对特定的XHR对象使用abort(),它不会全部中止!我不知道XHR是什么意思,我会用谷歌搜索它。谢谢您使用的是哪个版本的jquery?这是您的解决方案答案?是的,这是我的解决方案,但我在两点之前不能接受days@esimov您知道在刷新页面或更改页面时停止所有这些ajax请求的方法吗?当没有数据时,“函数(响应)”返回什么?我在想使用“error:function()”会有帮助吗?