I am trying to capture/know when it reached the 200(success) for the last and do something ie exit out.
How do i know that it has reached the 200 for the last time?
Here is my code below
//Main function whihc calls the save and checkstatus function
$(function () {
$("#saveChange").click(function () {
save().complete(function (jqXHR, responseText) {
var stat = jqXHR.status;
if (stat == 200) {
console.log("inside 200");
setTimeout(function () {
checkServerStatus().complete(function (jqXHR, responseText) {
if (jqXHR.status == 200) {
console.log("inside 200");
setTimeout(function () {
checkServerStatus().complete(function (jqXHR, responseText) {
count = 0;
count++;
max = 10;
if ((count <= max) && (jqXHR.status == 200)) {
checkServerStatus();
//alert(count);
}
});
if (jqXHR.status == 500) {
// alert(" 500 setimeout inside 500 status "+jqXHR.status + responseText);
} else if (jqXHR.status == 200) {
// alert(" 200 setimeout inside 200 status "+jqXHR.status + responseText);
}
}, 9000);
} else if (jqXHR.status == 500) {
// 500 error no submission
console.log("inside 500");
//alert(" 300 setimeout inside 500 status "+jqXHR.status + responseText);
checkServerStatus();
} else {
alert("else");
}
});
}, 6000);
}
});
});
});
});
There is a nice discussion about retrying ajax requests here . I would advise handling the deferreds yourself. This way you can make a dummy deferred, resolve or reject it at your whim, then chain as usual.
function checkServerStatus() {
var dfd = new $.deferred();
$.ajax({
url : 'ajaxurl.json',
tryCount : 0,
retryLimit : 3,
success : function(json) {
dfd.resolve();
},
error : function(xhr, textStatus, errorThrown ) {
if (this.tryCount++ <= this.retryLimit) {
//try again
$.ajax(this);
return;
}
dfd.reject();
}
});
return dfd;
}
checkServerStatus().then(function() {
// success!
}, function() {
// reached retry limit :(
});
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.