Reputation:
I am doing a script to retry a AJAX script in 7 seconds after it failed to load. Is there a way to display the timeleft in setTimeout?
this is my base script:
function update() {
$.ajax({
type: 'GET',
url: 'messageContent.php',
timeout: 1000,
success: function (data) {
$("#chatBox").html(data);
document.getElementById('messages').scrollTop = document.getElementById('messages').scrollHeight;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
var d = document.getElementById("loadcon");
d.innerHTML = '<div class="loadingAnimation"><h3 class="status">Reconnecting...</h3></div>';
setTimeout (update, 7000);
}
});
}
Upvotes: 4
Views: 427
Reputation: 1986
Try using a function to do the timer and call it on your handler
like this:
function startCounter(timeLeft) {
if (timeLeft > 0) {
..display timeLeft
setTimeout (function() {startCounter(timeLeft-1); }, 1000);
}
else{
..your function to retry
}
}
hope that helps :)
Upvotes: 2