Search code examples
javascripttimercountdowntimer

how to add and subtract time from a timer with a button in javascript


I am just learning javascript and playing around with a timer just for fun. I took a simple timer from w3schools and now am trying to add a + and - button to add and subtract time from the timer. I started with just the plus button and have it set to clearInterval which is working properly for stopping the time I just dont know where to go from there?

    <!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p {
  text-align: center;
  font-size: 60px;
  margin-top: 0px;
}
</style>
</head>
<body>

<p id="timer"></p>

<script>
// Set the date we're counting down to
var today = new Date();
var countDownDate = new Date(today.getTime() + (1 * 60 * 60 * 1000));

// Update the count down every 1 second
var x = setInterval(function() {


  // Get today's date and time
  var now = new Date().getTime();
    
  // Find the distance between now and the count down date
  var distance = countDownDate - now;
    
  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  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="timer"
  document.getElementById("timer").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";
    
  // If the count down is over, remove photo
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("timer").innerHTML = ""; 
  }
}, 1000);
</script>


<p id="timer" onclick="myFunction()">+</p>

<script>
function myFunction() {

clearInterval(x);

  document.getElementById("timer").innerHTML = "New Time";
  
  
}
</script>

</body>
</html> 

Solution

  • If you just want to add / remove time, there is no reason to clear the interval, just update the countDownDate and everything else will continue to work:

    // Set the date we're counting down to
    var today = new Date();
    var countDownDate = new Date(today.getTime() + (1 * 60 * 60 * 1000));
    const update = function() {
    
    
      // Get today's date and time
      var now = new Date().getTime();
        
      // Find the distance between now and the count down date
      var distance = countDownDate - now;
        
      // Time calculations for days, hours, minutes and seconds
      var days = Math.floor(distance / (1000 * 60 * 60 * 24));
      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="timer"
      document.getElementById("timer").innerHTML = days + "d " + hours + "h "
      + minutes + "m " + seconds + "s ";
        
      // If the count down is over, remove photo
      if (distance < 0) {
        clearInterval(x);
        document.getElementById("timer").innerHTML = ""; 
      }
    }
    // Update the count down every 1 second
    var x = setInterval(update, 1000);
    
    function addDay() {
      countDownDate.setDate(countDownDate.getDate()+1)
      update();
    }
    
    function subDay() {
      countDownDate.setDate(countDownDate.getDate()-1)
      update();
    }
    p {
      text-align: center;
      font-size: 60px;
      margin-top: 0px;
    }
    <p id="timer"></p>
    
    
    <button onclick="addDay()">+</button>
    <button onclick="subDay()">-</button>