Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let counter = 0.0;
  2. let myTimer;
  3.  
  4. function startTimer() {
  5.     let startBtn = document.querySelector('#timerButton');
  6.     startBtn.disabled = true;
  7.     counter = 0.0;
  8.     let tens = document.querySelector('#secondTens');
  9.     let ones = document.querySelector('#secondOnes');
  10.     let tenths = document.querySelector('#msTens');
  11.     let hundredths = document.querySelector('#msHundreds');
  12.     let match;
  13.     tens.textContent = '0';
  14.     ones.textContent = '0';
  15.     tenths.textContent = '0';
  16.     hundredths.textContent = '0';
  17.     myTimer = window.setInterval(function() {
  18.         if((counter + 0.01).toFixed(2) >= 10) stopTimer();
  19.         counter += 0.01;
  20.         match = counter.toFixed(2).toString().match(/^(\d)?(\d)?.(\d)?(\d)?/);
  21.         tens.textContent = match[2] === undefined ? '0' : match[1];
  22.         ones.textContent = match[2] === undefined ? match[1] : match[2];
  23.         tenths.textContent = match[3] === undefined ? '0' : match[3];
  24.         hundredths.textContent = match[4] === undefined ? '0' : match[4];
  25.     }, 10);
  26. }
  27.  
  28. function stopTimer() {
  29.     let startBtn = document.querySelector('#timerButton');
  30.     startBtn.disabled = false;
  31.     window.clearInterval(myTimer);
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement