我正在执行一个脚本,以便在AJAX脚本加载失败后7秒内重试它。有没有一种在setTimeout中显示时间点的方法?
这是我的基本脚本:
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);
}
});
}
发布于 2014-04-11 05:18:23
尝试使用一个函数来执行定时器,并在处理程序上调用它。
就像这样:
function startCounter(timeLeft) {
if (timeLeft > 0) {
..display timeLeft
setTimeout (function() {startCounter(timeLeft-1); }, 1000);
}
else{
..your function to retry
}
}
希望有帮助:)
https://stackoverflow.com/questions/23003971
复制相似问题