Home > OS >  How to convert a stopwatch to a count down timer in JS
How to convert a stopwatch to a count down timer in JS

Time:09-22

Is it possible to modify that code of a stopwatch bellow in a way that it works as a count down timer that starts from 00:13:00 and stops at 00:00:00. The start and reset button should function as before.

//Define vars to hold time values
let seconds = 0;
let minutes = 0;
let hours = 0;

//Define vars to hold "display" value
let displaySeconds = 0;
let displayMinutes = 0;
let displayHours = 0;

//Define var to hold setInterval() function
let interval = null;

//Define var to hold stopwatch status
let status = "stopped";

//Stopwatch function (logic to determine when to increment next value, etc.)
function stopWatch(){

    seconds  ;

    //Logic to determine when to increment next value
    if(seconds / 60 === 1){
        seconds = 0;
        minutes  ;

        if(minutes / 60 === 1){
            minutes = 0;
            hours  ;
        }

    }

    //If seconds/minutes/hours are only one digit, add a leading 0 to the value
    if(seconds < 10){
        displaySeconds = "0"   seconds.toString();
    }
    else{
        displaySeconds = seconds;
    }

    if(minutes < 10){
        displayMinutes = "0"   minutes.toString();
    }
    else{
        displayMinutes = minutes;
    }

    if(hours < 10){
        displayHours = "0"   hours.toString();
    }
    else{
        displayHours = hours;
    }

    //Display updated time values to user
    document.getElementById("display").innerHTML = displayHours   ":"   displayMinutes   ":"   displaySeconds;

}



function startStop(){

    if(status === "stopped"){

        //Start the stopwatch (by calling the setInterval() function)
        interval = window.setInterval(stopWatch, 1000);
        document.getElementById("startStop").innerHTML = "Stop";
        status = "started";

    }
    else{

        window.clearInterval(interval);
        document.getElementById("startStop").innerHTML = "Start";
        status = "stopped";

    }

}

//Function to reset the stopwatch
function reset(){

    window.clearInterval(interval);
    seconds = 0;
    minutes = 0;
    hours = 0;
    document.getElementById("display").innerHTML = "00:00:00";
    document.getElementById("startStop").innerHTML = "Start";

}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Stopwatch</title>
    <link href="https://fonts.googleapis.com/css2?family=Concert One&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="styles.css">


    <script type="text/javascript" src="scripts.js"></script>
    

</head>
<body>

    <div class="container">

        <div id="display">
            00:13:00
        </div>

        <div class="buttons">
            <button id="startStop" onclick="startStop()">Start</button> <button id="reset" onclick="reset()">Reset</button>
        </div>

    </div>
</body>
</html>

I have tried setting the initial time 00:13:00 and using a -- operator instead of operators. But it just shows negative time.

(Not including the CSS as it will stay the same)

CodePudding user response:

The if statements below //Logic to determine when to increment next value need to be changed to seconds < 0 and minutes < 0

//Define vars to hold time values
let seconds = 0;
let minutes = 1;
let hours = 0;

//Define vars to hold "display" value
let displaySeconds = 0;
let displayMinutes = 0;
let displayHours = 0;

//Define var to hold setInterval() function
let interval = null;

//Define var to hold stopwatch status
let status = "stopped";

//Stopwatch function (logic to determine when to increment next value, etc.)
function stopWatch() {

  seconds--;

  //Logic to determine when to increment next value
  if (seconds < 0) {
    if (minutes === 0 && hours === 0) {
      clearInterval(interval);
      alert("done!");
      return;
    } else {
      seconds = 59;
      minutes--;

      if (minutes < 0) {
        minutes = 59;
        hours--;
      }
    }

  }

  //If seconds/minutes/hours are only one digit, add a leading 0 to the value
  if (seconds < 10) {
    displaySeconds = "0"   seconds.toString();
  } else {
    displaySeconds = seconds;
  }

  if (minutes < 10) {
    displayMinutes = "0"   minutes.toString();
  } else {
    displayMinutes = minutes;
  }

  if (hours < 10) {
    displayHours = "0"   hours.toString();
  } else {
    displayHours = hours;
  }

  //Display updated time values to user
  document.getElementById("display").innerHTML = displayHours   ":"   displayMinutes   ":"   displaySeconds;

}



function startStop() {

  if (status === "stopped") {

    //Start the stopwatch (by calling the setInterval() function)
    interval = window.setInterval(stopWatch, 1000);
    document.getElementById("startStop").innerHTML = "Stop";
    status = "started";

  } else {

    window.clearInterval(interval);
    document.getElementById("startStop").innerHTML = "Start";
    status = "stopped";

  }

}

//Function to reset the stopwatch
function reset() {

  window.clearInterval(interval);
  seconds = 0;
  minutes = 13;
  hours = 0;
  document.getElementById("display").innerHTML = "00:00:00";
  document.getElementById("startStop").innerHTML = "Start";

}
<div class="container">

  <div id="display">
    00:01:00
  </div>

  <div class="buttons">
    <button id="startStop" onclick="startStop()">Start</button> <button id="reset" onclick="reset()">Reset</button>
  </div>

</div>

  • Related