VladoG

[JScore-JS-Advanced-Exercises] - 03. Timer

Oct 27th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function timer() {
  2.     let sec = 0;
  3.     let timer;
  4.     let startBtn = $('#start-timer');
  5.     let stopBtn = $('#stop-timer');
  6.     stopBtn.attr("disabled", true);
  7.  
  8.     startBtn.on("click", function (event) {
  9.         startBtn.attr("disabled", true);
  10.         stopBtn.attr("disabled", false);
  11.         timer = setInterval(step, 1000);
  12.         //alert("START");
  13.     });
  14.  
  15.     stopBtn.on("click", function (event) {
  16.         stopBtn.attr("disabled", true);
  17.         startBtn.attr("disabled", false);
  18.         clearInterval(timer);
  19.         //alert("STOP");
  20.     });
  21.  
  22.     function step() {
  23.         sec++;
  24.         let digSec = sec % 60;
  25.         let digMin = parseInt((sec/60)%60);
  26.         let digHour = parseInt(sec/3600);
  27.  
  28.         if (digSec < 10 ){
  29.             digSec = '0' + digSec.toString();
  30.         }
  31.         if (digMin < 10 ){
  32.             digMin = '0' + digMin.toString();
  33.         }
  34.         if (digHour < 10 ){
  35.             digHour = '0' + digHour.toString();
  36.         }
  37.  
  38.         $('#seconds').text(digSec);
  39.         $('#minutes').text(digMin);
  40.         $('#hours').text(digHour);
  41.  
  42.     }
  43. }
Add Comment
Please, Sign In to add comment