Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. function timer() {
  2. let running = false;
  3.  
  4. $('#start-timer').click(startTimer);
  5. $('#stop-timer').click(stopTimer);
  6.  
  7. let hours = $('#hours').text();
  8. let minutes = $('#minutes').text();
  9. let seconds = $('#seconds').text();
  10.  
  11. function startTimer() {
  12.  
  13. if (!running) {
  14. running = true;
  15. timer = setInterval(step, 1000);
  16.  
  17. function step() {
  18.  
  19. if (seconds >= 59) {
  20. seconds = 0;
  21. minutes++;
  22. $('#minutes').text(minutes < 10 ? ('0' + minutes.toString()) : minutes);
  23. }
  24. if (minutes >= 59) {
  25. minutes = 0;
  26. hours++;
  27. $('#hours').text(hours < 10 ? ('0' + hours.toString()) : hours);
  28. }
  29. seconds++;
  30. $('#seconds').text(seconds < 10 ? ('0' + seconds.toString()) : seconds);
  31. }
  32. }
  33. }
  34.  
  35. function stopTimer() {
  36. if (running) {
  37. running = false;
  38. clearInterval(timer);
  39. }
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement