使用HTML和CSS的JS中的倒数时钟

时间:2018-11-09 13:06:50

标签: javascript html css

我想显示一个倒数计时器和进度条。我有此javascript代码,但无法将css,html和进度条代码添加到我的javascript代码中。

<script>
var hoursleft =  3;
var minutesleft = 0; 
var secondsleft =  0; 
var finishedtext = "Countdown finished!";
var end1;
if(localStorage.getItem("end1")) {
end1 = new Date(localStorage.getItem("end1"));
} else {
end1 = new Date();
end1.setMinutes(end1.getMinutes()+minutesleft);
end1.setSeconds(end1.getSeconds()+secondsleft);}
var counter = function () {
var now = new Date();
var diff = end1 - now;
diff = new Date(diff);
	var milliseconds = parseInt((diff%1000)/100)
    var sec = parseInt((diff/1000)%60)
    var mins = parseInt((diff/(1000*60))%60)
    var hours = parseInt((diff/(1000*60*60))%24);
if (hours < 10) { 
    hours = "0" + hours;} 
if (mins < 10) {
    mins = "0" + mins;}
if (sec < 10) { 
    sec = "0" + sec;}     
if(now >= end1) {     
    clearTimeout(interval);
    localStorage.setItem("end", null);
     localStorage.removeItem("end1");
     localStorage.clear();
    document.getElementById('divCounter').innerHTML = finishedtext;
     if(confirm("TIME UP!"))
     window.location.href= "result.php";
} else {
    var value = hours+":" +mins + ":" + sec;
    localStorage.setItem("end1", end1);
    document.getElementById('divCounter').innerHTML = value;}}
var interval = setInterval(counter, 1000);
</script>

有人可以帮我添加带有此倒数计时器的css和progressbar,如图所示吗?

1 个答案:

答案 0 :(得分:1)

也许我不完全理解您的问题。但是,我想出了一些想法。

  • 也许代码的html部分会通过将其复制到Post中而丢失。

  • 我认为您了解Basics about adding a script to html code.

  • 我根据您的代码制作了一个超级简单的示例:

    <html>
      <body>
        <progress id="progressBar" value="22" max="100"></progress>
        <div id="divCounter"></div>
      </body>
    </html>
    
    <script>
    var hoursleft =  0;
    var minutesleft = 5;
    var secondsleft =  0;
    var finishedtext = "Countdown finished!";
    var end1;
    var progressBar = document.getElementById('progressBar')
    if(localStorage.getItem("end1")) {
      end1 = new Date(localStorage.getItem("end1"));
    } else {
      end1 = new Date();
      end1.setHours(end1.getHours()+hoursleft); // Why is this line missing?
      end1.setMinutes(end1.getMinutes()+minutesleft);
      end1.setSeconds(end1.getSeconds()+secondsleft);
    }
    progressBar.max = end1 - new Date();
    
    
    var counter = function () {
      var now = new Date();
      var diff = end1 - now;
      diff = new Date(diff);
      var milliseconds = parseInt((diff%1000)/100)
      var sec = parseInt((diff/1000)%60)
      var mins = parseInt((diff/(1000*60))%60)
      var hours = parseInt((diff/(1000*60*60))%24);
    
      if (hours < 10) {
          hours = "0" + hours;
        }
      if (mins < 10) {
          mins = "0" + mins;
        }
      if (sec < 10) {
          sec = "0" + sec;}
      if(now >= end1) {
        clearTimeout(interval);
        localStorage.setItem("end", null);
        localStorage.removeItem("end1");
        localStorage.clear();
        document.getElementById('divCounter').innerHTML = finishedtext;
        if(confirm("TIME UP!")) window.location.href= "result.php";
      } else {
          progressBar.value = progressBar.max - (end1-now);
          var value = hours+":" +mins + ":" + sec;
          localStorage.setItem("end1", end1);
          document.getElementById('divCounter').innerHTML = value;
      }
    }
    var interval = setInterval(counter, 1000);
    </script>
    

我希望能有所帮助。不要犹豫,让您的问题更加清楚。