Todorov_Stanimir

04. Stopwatch Lab: DOM Manipulations

Jan 13th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function stopwatch() {
  2.     let startButton = document.getElementById('startBtn');
  3.     startButton.addEventListener('click', start);
  4.     let stopButon = document.getElementById('stopBtn');
  5.     stopButon.addEventListener('click', stop);
  6.     let outputBox = document.getElementById('time');
  7.     let timer = null;
  8.     function stop() {
  9.         stopButon.disabled = true;
  10.         startButton.disabled = false;
  11.         clearInterval(timer);
  12.     }
  13.     function start() {
  14.         startButton.disabled = true;
  15.         stopButon.disabled = false;
  16.         let seconds = 0;
  17.         timer = setInterval(tick, 1000);
  18.         outputBox.textContent = '00:00';
  19.        
  20.         function tick() {
  21.             seconds++;
  22.             let currentMinute = ('0' + Math.floor(seconds / 60)).slice(-2);
  23.             let currentSecond = ('0' + seconds % 60).slice(-2);
  24.             outputBox.textContent = `${currentMinute}:${currentSecond}`;
  25.         }
  26.     }
  27. }
Add Comment
Please, Sign In to add comment