现在已经过了整整一天,我似乎无法解决这个问题。是否有可能更新函数内的间隔计时器?
我在为我的网站工作,我只需要知道如何在自己的时间间隔内从IF语句更改间隔计时器。
间隔将检查$ .post是否有错误,如果有错误,它将显示第一个系统错误。 (以及增加另一个变量以进行错误#2
3秒钟后,将显示错误#2,这正是我想要的,但间隔持续保持在3秒,并且无论如何不工作我做的。
_staffIntervalID = 3000;
serverError = 0;
staffMsg = setInterval(function() {
if(isInChat == true) {
$.post('./slivechat.php', {staff:"true"}, function(data) {
if(data == "") return;
$('#display_msg').append(+ nl2br(data) + "<br />");
$('#display_msg').animate({scrollTop: $(document).height()}, 'slow');
})
.error(function() {
serverError++;
if(serverError == 1) {
$('#display_msg').append("<span style='color: red;'>System</span>: An error occured whilst we tried to fetch staff response. Please wait while I try to fix the this problem.<br />");
_staffIntervalID = 12000;
} else if(serverError == 2) {
$('#display_msg').append("<span style='color: red;'>System</span>: There seems to be an error, please check your conection. Or wait a few more seconds for the final verdict.<br />");
_staffIntervalID = 24000;
} else if(serverError >= 3) {
$('#display_msg').append("<span style='color: red;'>System</span>: Due to technical difficulties we are closing this chat. Please try again later! <br />");
clearInterval(staffMsg);
}
$('#display_msg').animate({scrollTop: $(document).height()}, 'slow');
console.log("ServerError: " + serverError + " timer: " + _staffIntervalID);
});
}
}, _staffIntervalID);
即使在CONSOLE.LOG它表明计时器发生了变化,但它一直在更新每隔3
真的希望我能得到这个尽快解决!
PS-的Javascript不是我的主要语言,PHP是。所以如果你有一个修复,请尝试举一个例子。
使用setTimeout而不是setInterval并根据您的条件更改持续时间。 – Virus721