Hi guys please check this code, first part from innit, second is my timer function. Thanks alot!
I set up a timer for 15min. Save the time in cookies and read saved time on init
var cc=readCookie("agreement");
var ct=readCookie("secondsleft");
if (ct == null || ct == '') {
secondsleft = 15*60;
} else {
secondsleft = parseInt(ct);
}
myInterval = setInterval(bingoTimer,1000);
Can anyone see why after refresh this code jumps to 0:13?
function bingoTimer() {
secondsleft--;
if (secondsleft < 0) {
} else {
var minLeft = parseInt(secondsleft / 60);
var secLeft = secondsleft - minLeft * 60;
var timeLeft = " " + minLeft+":" + secLeft;
var timeCookie = createCookie("secondsleft", timeLeft);
$('#timer').html(timeLeft);
}
}
I believe parseInt parses the first integer value that it can find in the string. In your example you save the time int this format: “minLeft:secLeft”. I think parseInt will only parse the minLeft part and ignores the rest of the string after the ‘:’. So your 13 minute and something seconds is parsed into 13 seconds in secondsleft = parseInt(ct);
Try changing createCookie(“secondsleft”, timeLeft) to createCookie(“secondsleft”, secondsleft.toString()).