Как насчет чего-то подобного. Инициировать, отслеживать и перезапускать опрос в рамках обещаний ajax.
var pollingTimer = null, // stores reference to the current timer id
firstTimeoutResponse = null; // stores the start of what might be a series of timeout responses
function poll(){
$.ajax({
// your options here...
}).done(function() {
// reset the "timeout" timer
firstTimeoutResponse = null;
}).fail(function(jqXHR, textStatus) {
// if the failure wasn't a timeout, short-circuit,
// but only after resetting the timeout timestamp
if (textStatus !== 'timeout') {
firstTimeoutResponse = null;
return;
}
// if it was a timeout failure, and the first one (!), init the timeout count
if (firstTimeoutResponse = null) {
firstTimeoutResponse = (new Date).getTime();
}
}).always(function() {
// if 2 min have passed and we haven't gotten a good response, stop polling/chort-circuit
if ((new Date).getTime() - firstTimeoutResponse > 120000) { // 120000ms = 2min
window.clearTimeout(pollingTimer);
return;
}
// queue the next ajax call
pollingTimer = window.setTimeout(poll, 3000); // poll every 3s
});
}
// kick things off!
poll();
Первым делом необходимо записать текущее время ('new Date()') в каждом опросе на некотором уровне повышенной области. Затем проверьте разницу между ранее сохраненным временем до принятия решения. – ne1410s
Возможно, вас заинтересует http://stackoverflow.com/questions/3543683/determine-if-ajax-error-is-a-timeout, в котором объясняется, как проверить ответ ajax для ошибок таймаута. Просто запланируйте новый опрос на .success и в .error проверьте, была ли это ошибка таймаута - если да, то идите. –