Advertisement
ralitsa_d

Untitled

Oct 23rd, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. function timer() {
  2. let startBtn = $("#start-timer");
  3. let stopBtn = $("#stop-timer");
  4.  
  5. let hours = $("#hours");
  6. let minutes = $("#minutes");
  7. let seconds = $("#seconds");
  8.  
  9. let interval = undefined;
  10. let totalSeconds = 0;
  11.  
  12. startBtn.on("click", function () {
  13. if (interval == undefined){
  14. interval = setInterval(step, 1000);
  15. }
  16. });
  17.  
  18. stopBtn.on("click", function () {
  19. clearInterval(interval);
  20. interval = undefined;
  21. });
  22.  
  23. function step() {
  24. //console.log("kur");
  25. totalSeconds++;
  26.  
  27. seconds.text(formatTime(totalSeconds % 60));
  28. let mins = totalSeconds / 60;
  29. minutes.text(formatTime(mins % 60));
  30. let h = totalSeconds / 3600;
  31. hours.text(formatTime(h));
  32. }
  33.  
  34. function formatTime(num) {
  35. //console.log("kur");
  36. return pad(Math.floor(num));
  37. }
  38.  
  39. function pad(num) {
  40. if (num <= 9){
  41. num = `0${num}`;
  42. }
  43. return num;
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement