sonia
sonia

Reputation: 49

Countdown clock in JS using HTML and CSS

I want to display a countdown timer and progress bar. I have this javascript code but I can not add css, html and progress bar code to my javascript code.

<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>

can someone please help me to add css and progressbar with this countdown timer as shows the picture??

Upvotes: 0

Views: 536

Answers (1)

TheNewCivilian
TheNewCivilian

Reputation: 109

Maybe I don't understand your question completely. But here are some ideas I would come up with.

  • maybe the html part of your code got lost by copying it into your Post.

  • I assume you understood the Basics about adding a script to html code.

  • I made a super simple example based on your 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>
    

I hope that helps. Don't hesitate to make your question more clear.

Upvotes: 1

Related Questions