Reputation: 41
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.
Upvotes: 1
Views: 161
Reputation: 2275
Actually the setInterval method won't change the timer value by just reseting the timer value.
What you should do is clear the timer and use a function name as a reference for your function like :
var delay = 1000, now = new Date().valueOf();
var timer = setInterval(function loop()
{
console.log(now - new Date.valueOf());
delay += 1000;
clearInterval(timer);
timer = setInterval(loop, delay);
}, delay);
or (with a timeout) :
var delay = 1000, now = new Date().valueOf();
var timer = setTimeout(function loop()
{
console.log(now - new Date.valueOf());
delay += 1000;
timer = setTimeout(loop, delay);
}, delay);
Note that putting an ajax call in a timer might be a good idea. You should rather wait for the response to call again.
Upvotes: 0
Reputation: 9224
I would recommend not using an anonymous function and resetting the timer
serverError = 0;
var staffMsg = setInterval(CheckError, 3000);
function CheckError () {
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 />");
clearTimeout(staffMsg);
staffMsg = setInterval(CheckError, 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 />");
clearTimeout(staffMsg);
staffMsg = setInterval(CheckError, 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);
});
}
}
Upvotes: 1
Reputation: 96
Even if you change the variable _staffIntervalID does not mean that the setInterval is gonna change, in order to make it work you need to do a clearInterval and run a new setInterval with the new value
Upvotes: 0
Reputation: 324650
No, you can't change the interval period.
But you can do it this way instead:
var _staffIntervalID = 3000;
setTimeout(function() {
// your code here
setTimeout(arguments.callee,_staffIntervalID);
},_staffIntervalID);
Upvotes: 2