Advertisement
Guest User

Untitled

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