I have a problem with the if-else statement in the code given below. The code below shows a reverse counter from 15 seconds. Now when the counter hits to 10 seconds, it should show a message ("You will be logged out in 10 seconds"). The counter shouldn't stop and when it hits 0 seconds, it should redirect to some other page. The below code works till the time it reaches 10 seconds and shows the message. After that, it just stops. How can I make it work... (add break, continue in the code?)
function setCookie(cname,cvalue,exdays)
{
var d = new Date();
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++)
{
var c = ca[i].trim();
if (c.indexOf(name)==0) return c.substring(name.length,c.length);
}
return "";
}
//check existing cookie
cook=getCookie("my_cookie");
if(cook==""){
//cookie not found, so set seconds=60
var seconds = 15;
}else{
seconds = cook;
console.log(cook);
}
function secondPassed() {
var minutes = Math.round((seconds - 30)/60);
var remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
remainingSeconds = "0" + remainingSeconds;
}
//store seconds to cookie
setCookie("my_cookie",seconds,5); //here 5 is expiry days
document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
if (seconds == 10) {
clearInterval(countdownTimer);
document.getElementById('countdown').innerHTML = "You will be logged out in 10 seconds";
} else if (seconds == 0) {
clearInterval(countdownTimer);
document.getElementById('countdown').innerHTML = "Buzz Buzz";
window.location="http://www.google.com";
} else {
seconds--;
}
}
var countdownTimer = setInterval(secondPassed, 1000);
Here is the working code.. http://ift.tt/1yIwlyK
Aucun commentaire:
Enregistrer un commentaire