Advertisement
vaakata

Timer_21.10.16

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