Guest User

Stopwatch

a guest
Dec 31st, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function stopwatch() {
  2.      let seconds = 0, minutes = 0, t;
  3.  
  4.      let time = document.getElementById('time');
  5.      let startbtn = document.getElementById('startBtn');
  6.      let stopbtn = document.getElementById('stopBtn');
  7.  
  8.      startbtn.addEventListener("click", function(){
  9.         t = setInterval(add, 1000);
  10.         stopbtn.disabled = false;
  11.         startbtn.disabled = true;
  12.      
  13.     });
  14.    
  15.      stopbtn.addEventListener("click", function(){
  16.         clearInterval(t);
  17.         seconds = "00";
  18.         minutes = "00";
  19.         time.textContent = `${minutes}:${seconds}`;
  20.         stopbtn.disabled = true;
  21.         startbtn.disabled = false;
  22.       });
  23.  
  24.     function add() {  
  25.         seconds++;  
  26.         if(seconds<10){
  27.             seconds=`0` + seconds;
  28.         }
  29.          
  30.         if (seconds >= 60) {
  31.             seconds = `0` + 0;
  32.             minutes++;
  33.             if(minutes<10){
  34.                minutes=`0` + minutes;
  35.             }
  36.         }
  37.         if(minutes===0){
  38.             minutes=`0` + minutes;
  39.          }
  40.         time.textContent = `${minutes}:${seconds}`;
  41.      
  42.     }  
  43.  
  44. }
Add Comment
Please, Sign In to add comment