Search code examples
javascripthtmlcountdowntimer

Javascript countdown timer not working correctly


I'm creating a javascript function to countdown a user selected value. This is my code so far.

function countdownTimeStart() {
  var time = document.getElementById("test").value;
  time = time.split(':');
  var date = new Date();
  var countDownDate = date.setHours(time[0], time[1], time[2]);
  var x = setInterval(function () {
    // Get to days date and time
    var now = new Date().getTime();

    // Find the distance between now an the count down date
    /* var distance = countDownDate;*/
    var distance = countDownDate - now;

    // Time calculations for days, hours, minutes and seconds
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    // Output the result in an element with id="demo"
    document.getElementById("demo1").innerHTML = hours + ": "
      + minutes + ": " + seconds + " ";

    // If the count down is over, write some text
    if (distance < 0) {
      clearInterval(x);
      document.getElementById("demo1").innerHTML = "00:00:00";
    }
  }, 200);
}
document.querySelector("button").addEventListener(
  "click",
  countdownTimeStart
)
<input type = "test" id = "test" value="20:12:40">
<div id="demo1"></div>
<button>start</button>

The problem is countdown is not working properly. The count down time start from another time instead of start from 20:12:40 which comes from input tag.

Please help me to solve this.


Solution

  • if you would like to count down a given time the following should work:

    var time = document.getElementById("test").value;
    time = time.split(':');
    var date = new Date();
    var countDownDate = date.setHours(time[0], time[1], time[2]);
    
    function countdownTimeStart() {
      var x = setInterval(function () {
    
        // set hours, minutes and seconds, decrease seconds
        var hours = time[0];
        var minutes = time[1];
        var seconds = time[2]--;
        
        // if seconds are negative, set them to 59 and reduce minutes
        if (time[2] == -1) {
        	time[1]--;
            time[2] = 59
        }
        
        // if minutes are negative, set them to 59 and reduce hours
        if (time[1] == -1) {
        	time[0]--;
            time[1] = 59
        }
    
        // Output the result in an element with id="demo"
        // add leading zero for seconds if seconds lower than 10
        if (seconds < 10) {
            document.getElementById("demo").innerHTML = hours + ": " + minutes + ": " + "0" + seconds + " ";
        } else {
            document.getElementById("demo").innerHTML = hours + ": " + minutes + ": " + seconds + " ";
        }
      }, 1000);
    }
    
    document.querySelector("button").addEventListener(
      "click",
      countdownTimeStart
    )
    <input type = "text" id = "test" value="20:00:01">
    <div id="demo"></div>
    <button>start</button>