Is it possible to update the interval timer within the function?
It's been a full day now and I still can't seem to fix this issue.
I working on a project for my website and I just need to know how to
change the interval timer from an IF statement within the interval it
self.
The interval will check if there is an error from the $.post and if there
is, it will display the first system error. (aswell as incrementing
another variable to proceed to error #2.
3 seconds later, error #2 will show. That's exactly how I want it.
However, the interval keeps staying at 3 seconds and it's not working no
matter what I do.
_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);
Even on console.log it shows that the timer has changed, but it keeps
updating every 3.
Really hoping I can get this fixed soon!
Ps- Javascript is not my main language, PHP is. So if you do have a fix,
please try give an example too.
No comments:
Post a Comment