给定以下jQuery代码
function poll(){
$.ajax({ url: /myurl,
success: function(data){
//do stuff
},
dataType: "json",
complete: poll,
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
$("#ajax-msg").html("Not connect.n Verify Network.");
} else if (jqXHR.status == 404) {
$("#ajax-msg").html("Requested page not found. [404]");
} else if (jqXHR.status == 500) {
$("#ajax-msg").html("Internal Server Error [500]");
} else if (exception === 'parsererror') {
$("#ajax-msg").html("Requested JSON parse failed.");
} else if (exception === 'timeout') {
$("#ajax-msg").html("Time out error.");
} else if (exception === 'abort') {
$("#ajax-msg").html("Ajax request aborted.");
} else {
$("#ajax-msg").html("Uncaught Error.n" + jqXHR.responseText);
}
//wait for some interval and then try to poll again
},
timeout: 10000
});
}
在错误条件下,我希望poll()函数在1s, 2s, 4s, 8s, 16s, 32s, 1m, 2m, 4m, 10m, 20m, 30m, 40m…
我读到使用睡眠是不正确的。我想知道"睡眠"的正确方法,设置间隔或超时来完成这一点。
直接使用setTimeout并重新发送请求。
function poll() {
var delay = 1000,
failnum = 0;
$.ajax({
url: /myurl,
success: function(data){
/ / do stuff
},
dataType: "json",
complete: poll,
error: function (jqXHR, exception) {
if (jqXHR.status === 0) {
$("#ajax-msg").html("Not connect.n Verify Network.");
} else if (jqXHR.status == 404) {
$("#ajax-msg").html("Requested page not found. [404]");
} else if (jqXHR.status == 500) {
$("#ajax-msg").html("Internal Server Error [500]");
} else if (exception === 'parsererror') {
$("#ajax-msg").html("Requested JSON parse failed.");
} else if (exception === 'timeout') {
$("#ajax-msg").html("Time out error.");
} else if (exception === 'abort') {
$("#ajax-msg").html("Ajax request aborted.");
} else {
$("#ajax-msg").html("Uncaught Error.n" + jqXHR.responseText);
}
//wait for some interval and then try to poll again
var opts = this;
failnum++;
setTimeout(function () {
$.ajax(opts);
}, failnum * delay * 2);
},
timeout: 10000
});
}
你当然可以修改failnum * delay * 2
来获得你想要的每次失败的延迟
我认为最简单的方法是在成功和错误条件下使用相同的机制来继续轮询。
应该这样做:
function poll(){
clearTimeout(poll.data.timeout);
$.ajax({ url: /myurl,
success: function(data){
//do stuff
poll.data.index = 0;
},
dataType: "json",
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
$("#ajax-msg").html("Not connect.n Verify Network.");
} else if (jqXHR.status == 404) {
$("#ajax-msg").html("Requested page not found. [404]");
} else if (jqXHR.status == 500) {
$("#ajax-msg").html("Internal Server Error [500]");
} else if (exception === 'parsererror') {
$("#ajax-msg").html("Requested JSON parse failed.");
} else if (exception === 'timeout') {
$("#ajax-msg").html("Time out error.");
} else if (exception === 'abort') {
$("#ajax-msg").html("Ajax request aborted.");
} else {
$("#ajax-msg").html("Uncaught Error.n" + jqXHR.responseText);
}
poll.data.index = Math.min(++poll.data.index, poll.data.delays.length-1);
},
complete: function( jqXHR, textStatus){
//wait for some interval and then try to poll again
poll.data.timeout = setTimeout(poll, poll.data.delays[poll.data.index] * 1000);
},
timeout: 10000
});
}
//And now a tidy place to keep the data for managing the poll.
poll.data = {
delays: [0, 1, 2, 4, 8, 16, 32, 60, 120, 240, 600, 1200, 1800, 2400],//seconds
index: 0,
timeout
};
延迟后的递归轮询调用可以工作:
function poll(delay){
...
error: function(jqXHR, exception) {
...
//wait for some interval and then try to poll again
setTimeout(function() {
poll(delay ? 2 * delay : 1000);
},
delay ? delay : 1000);
},
timeout: 10000
});
}
但它永远不会停止尝试,直到你关闭浏览器或电脑。但这是你要求的