Given the following jQuery code
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
});
}
On an error condition, I would like the poll() function to be called again in 1s, 2s, 4s, 8s, 16s, 32s, 1m, 2m, 4m, 10m, 20m, 30m, 40m...
I have read that using a sleep is not correct. I would like to know the proper way to 'sleep', set an interval, or timeout to accomplish this.