Guest User

Stopwatch

a guest
Dec 31st, 2019
247
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 = null;
  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.         seconds = '00';
  10.         minutes = '00';
  11.         time.textContent = `${minutes}:${seconds}`;
  12.         t = setInterval(add, 1000);
  13.         stopbtn.disabled = false;
  14.         startbtn.disabled = true;
  15.     });
  16.  
  17.     stopbtn.addEventListener('click', function() {
  18.         stopbtn.disabled = true;
  19.         startbtn.disabled = false;
  20.         clearInterval(t);
  21.     });
  22.  
  23.     function add() {
  24.         seconds++;
  25.         if (seconds < 10) {
  26.             seconds = `0` + seconds;
  27.         }
  28.  
  29.         if (seconds >= 60) {
  30.             seconds = `0` + 0;
  31.             minutes++;
  32.             if (minutes < 10) {
  33.                 minutes = `0` + minutes;
  34.             }
  35.         }
  36.         if (minutes === 0) {
  37.             minutes = `0` + minutes;
  38.         }
  39.         time.textContent = `${minutes}:${seconds}`;
  40.     }
  41. }
Add Comment
Please, Sign In to add comment