I have multiple ajax requests and when one of them can't get data I want , I re-send it until it can get data . the problem that I can't stop it after it gets data . Is there's a break or something equivalent to it in ajax ? I tried clearinterval but it didn't work here's my functions :
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;
}
I call it :
ajaxGetDatabase('tab_backup',ips,3000);
var timer = null;
function ajaxGetServerDatabase(Div,val,interval){
//...
if (response!='no_connection'){
dbs[val]=JSON.parse(response)
clearInterval(timer); // ????
}
//....
else
{
timer = setInterval("ajaxGetServerDatabase('"+Div+"','"+val+"','"+interval+"')", interval);
}
clearInterval
has nothing to do with ajax
. It's only a timer function which scope is to clear the timer set earlier with setInterval
. If you really want to use a timer function you need either to attach a variable to the setInterval
function, which you can clear with clearInterval
setting as a parameter the id defined earlier in the setInterval
.
var id = " ";
success: function(response) {
if (response!='no_connection'){
dbs[val]=JSON.parse(response)
clearInterval(id);
}
else
id= setInterval("ajaxGetServerDatabase('"+Div+"','"+val+"','"+interval+"')", interval);
}
Or you can abort the code with ajax abort
.
Maybe something close to this?
...
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);
}
}
});
}
here's the answer : in ajaxGetDatabase :
success: function(response) {
if (response!='no_connection'){
dbs[val]=JSON.parse(response)
}
else
{
id=setInterval("ajaxGetServerDatabase('"+Div+"','"+val+"')", interval);
}
in ajaxGetServerDatabase :
success: function(response) {
if (response!='no_connection'){
dbs[val]=JSON.parse(response)
clearInterval(id);
}
}
with out scope parameter
var id;
to make it general and work for more than one server had stopped (more than one ajax request is failed) I used an array to save ids like this :
var ids=new Array();
ids[val]=setInterval("ajaxGetServerDatabase('"+Div+"','"+val+"')", interval);
clearInterval(ids[val]);
Are you looking for the timeout
setting perhaps? eg
$.ajax({
timeout: 10000,
...
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.