簡體   English   中英

javascript中的多個if-else語句,用於計時器

[英]Multiple if-else statement in javascript for timer

我在下面的代碼中使用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/

  if (seconds == 10) {
            clearInterval(countdownTimer);
            document.getElementById('countdown').innerHTML = "You will be logged out in 10 seconds";
  ...

clearInterval停止計時器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM