简体   繁体   English

javascript中未显示倒数计时器

[英]Countdown Timer is not showing in javascript

I am new in javascript, I want to create a countdown timer with localStorage which starts from given time and end to 00:00:00, but it's not working, When I am running my code it is showing value "1506". 我是Java语言的新手,我想用localStorage创建一个倒计时计时器,该计时器从给定时间开始,到00:00:00结束,但是它不起作用,当我运行我的代码时,它显示值“ 1506”。

Here is my code 这是我的代码

<script type="text/javascript">
            if (localStorage.getItem("counter")) {
                var CurrentTime = localStorage.getItem("counter");
            }
            else {
                var Hour = 3;
                var Minute = 25;
                var Second = 60;
                var CurrentTime = Hour.toString() + ":" + Minute.toString() + ":" + Second.toString();
            }


            function CountDown() {                                
                document.getElementById('lblDuration').innerHTML = CurrentTime;
                Second--;
                if (Second == -1) {
                    Second = 59;
                    Minute--;
                }
                if (Minute == -1) {
                    Minute = 59;
                    Hour--;
                }
                localStorage.setItem("counter", CurrentTime);
            }

            var interval = setInterval(function () { CountDown(); }, 1000);

    </script>
  1. you need to declare variables Hour, Minute, Second, CurrentTime out side if else block. 您需要在变量if else声明变量Hour, Minute, Second, CurrentTime In this case they are not in function CountDown() scope. 在这种情况下,它们不在function CountDown()范围内。
  2. you are not setting CurrentTime = Hour.toString() + ":" + Minute.toString() + ":" + Second.toString(); 您没有设置CurrentTime = Hour.toString() + ":" + Minute.toString() + ":" + Second.toString(); after localStorage.setItem("counter", CurrentTime); localStorage.setItem("counter", CurrentTime);

 var Hour = 3; var Minute = 25; var Second = 60; var CurrentTime = Hour.toString() + ":" + Minute.toString() + ":" + Second.toString(); function CountDown() { document.getElementById('lblDuration').innerHTML = CurrentTime; Second--; if (Second == -1) { Second = 59; Minute--; } if (Minute == -1) { Minute = 59; Hour--; } CurrentTime = Hour.toString() + ":" + Minute.toString() + ":" + Second.toString(); } setInterval(function () { CountDown(); }, 1000); 
 <div id="lblDuration"></div> 

When localStorage is available you don set the values for Hour, Minute and Second. 当localStorage可用时,您无需设置小时,分钟和秒的值。 So when the countdown function executed it finds Second to be undefined and the statement Second-- converts Second to NaN. 因此,执行倒数计时功能时,它会发现Second未定义,并且语句Second--将Second转换为NaN。 To fix it just initialize the Hour, Minute and Second Variable. 要解决此问题,只需初始化小时,分钟和秒变量。 I 've refactored your code a little bit hope it helps: 我对您的代码进行了重构,希望对您有所帮助:

function CountDown() { 
    var currentTime =  getCurrentTime();                           
    printCurrentTime(currentTime)
    currentTime.second--;
    if (currentTime.second == -1) {
        currentTime.second = 59;
        currentTime.minute--;
    }
    if (currentTime.minute == -1) {
        currentTime.minute = 59;
        currentTime.hour--;
    }
    setCurrentTime(currentTime);
}

  function setCurrentTime(newCurrentTime){
    if(localStorage) localStorage.setItem("counter", JSON.stringify(newCurrentTime));
    else setCurrentTime.storage = newCurrentTime;
  }
  function getCurrentTime(){
    var result = localStorage ? localStorage.getItem("counter") : setCurrentTime.storage;
    result = result || {hour:3, minute:25, second:60};
    if (typeof(result) === "string")result = JSON.parse(result);
    result.toString = function(){
        return result.hour +  ":" + result.minute + ":" + result.second;
    }
    return result;
  }
  function printCurrentTime(currentime){
        var domTag = document.getElementById('lblDuration');
        if(domTag) domTag.innerHTML = currentime.toString();
        else console.log(currentime);
  }
setInterval(function () { CountDown(); }, 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM