gadjov

Timer

Oct 26th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function timer() {
  2.         let time, intervalID;
  3.         let startBtn = $("#start-timer");
  4.         let stopBtn = $("#stop-timer");
  5.         let isPaused = false;
  6.         startBtn.click(function () {
  7.             if (isPaused == false) {
  8.                 time = -1;
  9.             }
  10.             else {
  11.                 time -= 1;
  12.             }
  13.             incrementTime();
  14.             intervalID = setInterval(incrementTime, 1000);
  15.             startBtn.attr('disabled', true);
  16.             stopBtn.attr('disabled', false);
  17.         });
  18.         stopBtn.on('click', function () {
  19.             clearInterval(intervalID);
  20.             isPaused = true;
  21.             startBtn.attr('disabled', false);
  22.             stopBtn.attr('disabled', true);
  23.         });
  24.         function incrementTime() {
  25.             time++;
  26.             $("#seconds").text(('0' + (time % 60)).slice(-2));
  27.             $("#minutes").text(('0' + Math.trunc(time / 60) % 60).slice(-2));
  28.             $("#hours").text(('0' + Math.floor(time / 3600) % 24).slice(-2))
  29.         }
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment