Search code examples
javascripttimercountdown

Javascript Countdown Timer with User input


I am trying to create a countdown timer where the user can input any combination of day, hour, minute, and seconds values and have it countdown to completion. I feel like I've got all the pieces but after a week of trying to solve this issue, I need help. I shuffle code around all with varying effects. Basically I feel like I'm missing a way to format the input data in a way I can use to subtract from the current date, but honestly I have no idea. Any input would be helpful.

Javascript:

function start() {
  var countdownTimer = setInterval('timer()', 1000);
}

function timer() {
  var d = parseInt(document.getElementById("days").value, 0);
  var h = parseInt(document.getElementById("hours").value, 0);
  var m = parseInt(document.getElementById("minutes").value, 0);
  var s = parseInt(document.getElementById("seconds").value, 0);

  var now = new Date();
  var date = now.getTime();

  addDay = now.setDate(now.getDate() + d);
  addHour = now.setHours(now.getHours() + h);
  addMinute = now.setMinutes(now.getMinutes() + m);
  addSecond = now.setSeconds(now.getSeconds() + s);

  var then = new Date(addHour + addMinute + addSecond);

  if(d > 0 || h > 0 || m > 0 || s > 0){
    var final = then - date;
    var dd = Math.floor(final/ (1000 * 60 * 60 * 24));
    var hh = Math.floor((final / (1000 * 60 * 60)) % 24);
    var mm = Math.floor((final / 1000 / 60) % 60);
    var ss = Math.floor((final / 1000) % 60);

    document.getElementById("display").innerHTML = "Time Remaining: " + dd + "D     " + hh + "H " + mm + "M " + ss + "S";

    document.getElementById("message").innerHTML = then;

    if (final < 0) {
      clearInterval(countdownTimer);
      document.getElementById("message").innerHTML = "Expired";
    }
  }else{
    document.getElementById("display").innerHTML = " ";
    document.getElementById("message").innerHTML = "Countdown Not Started";
  }
}

HTML:

<div id="countdowntimer">
  <button id="Start" onclick="start();">Start Timer</button>

  D:<input type="text" id="days" value="0" /> 
  H:<input type="text" id="hours" value="0" /> 
  M:<input type="text" id="minutes" value="0" />
  S:<input type="text" id="seconds" value="0" /><br>

  <div id="display"></div>

  <div id="message"></div>
</div>

Solution

  • If your timer is not based on date but on a given number of days, hours, minutes and seconds, why involve dates at all ? How about

    function timer(){
        var d = parseInt(document.getElementById("days").value, 0);
        var h = parseInt(document.getElementById("hours").value, 0);
        var m = parseInt(document.getElementById("minutes").value, 0);
        var s = parseInt(document.getElementById("seconds").value, 0);
    
        var current = ((d * 86400) + (h * 3600) + (m * 60) + s);  //the current time left in seconds
        if (current > 0) {
            //take one second away, and rerender the seconds split into d, h, m, and s in the html, which you will reuse next time timer() runs
        } else {
            //expired
        }
    }