我在下面给出的代码中遇到了if-else语句的问题。下面的代码显示了15秒的反向计数器。现在,当计数器达到10秒时,它应显示一条消息(“你将在10秒后退出”)。计数器不应该停止,当它达到0秒时,它应该重定向到其他页面。以下代码一直有效,直到达到10秒并显示消息。在那之后,它就会停止。我怎样才能使它工作......(在代码中添加中断,继续?)
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);
以下是工作代码.. http://jsfiddle.net/e80erzk8/
答案 0 :(得分:0)
if (seconds == 10) {
clearInterval(countdownTimer);
document.getElementById('countdown').innerHTML = "You will be logged out in 10 seconds";
...
clearInterval停止计时器