Advertisement
KageKarasu

Untitled

Jun 24th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Define vars to hold time values
  2. function stopWatch() {
  3.   let seconds = 0;
  4.   a;
  5.   let minutes = 0;
  6.   let hours = 0;
  7.  
  8.   //Define vars to hold "display" value
  9.   let displaySeconds = 0;
  10.   let displayMinutes = 0;
  11.   let displayHours = 0;
  12.  
  13.   //Define var to hold setInterval() function
  14.   let interval = null;
  15.  
  16.   //Define var to hold stopwatch status
  17.   let status = "stopped";
  18.  
  19.   //Stopwatch function (logic to determine when to increment next value, etc.)
  20.   function stopWatch() {
  21.     seconds++;
  22.  
  23.     //Logic to determine when to increment next value
  24.     if (seconds / 60 === 1) {
  25.       seconds = 0;
  26.       minutes++;
  27.  
  28.       if (minutes / 60 === 1) {
  29.         minutes = 0;
  30.         hours++;
  31.       }
  32.     }
  33.  
  34.     //If seconds/minutes/hours are only one digit, add a leading 0 to the value
  35.     if (seconds < 10) {
  36.       displaySeconds = "0" + seconds.toString();
  37.     } else {
  38.       displaySeconds = seconds;
  39.     }
  40.  
  41.     if (minutes < 10) {
  42.       displayMinutes = "0" + minutes.toString();
  43.     } else {
  44.       displayMinutes = minutes;
  45.     }
  46.  
  47.     if (hours < 10) {
  48.       displayHours = "0" + hours.toString();
  49.     } else {
  50.       displayHours = hours;
  51.     }
  52.  
  53.     //Display updated time values to user
  54.     document.getElementById("display").innerHTML =
  55.       displayHours + ":" + displayMinutes + ":" + displaySeconds;
  56.   }
  57. }
  58.  
  59. function startStop() {
  60.   if (status === "stopped") {
  61.     //Start the stopwatch (by calling the setInterval() function)
  62.     interval = window.setInterval(stopWatch, 1000);
  63.     document.getElementById("startStop").innerHTML = "Stop";
  64.     status = "started";
  65.   } else {
  66.     window.clearInterval(interval);
  67.     document.getElementById("startStop").innerHTML = "Start";
  68.     status = "stopped";
  69.   }
  70. }
  71.  
  72. //Function to reset the stopwatch
  73. function reset() {
  74.   window.clearInterval(interval);
  75.   seconds = 0;
  76.   minutes = 0;
  77.   hours = 0;
  78.   document.getElementById("display").innerHTML = "00:00:00";
  79.   document.getElementById("startStop").innerHTML = "Start";
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement