Advertisement
YavorJS

02. Timer

Jun 25th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. function timer() {
  2. let tick = 0;
  3. let clockOn = false;
  4. let myInterval = null;
  5.  
  6.  
  7. function step() {
  8. tick++;
  9. let seconds = ('0' + tick % 60).slice(-2);
  10. $('#seconds').text(seconds);
  11. let minutes = ('0' + Math.floor(tick/ 60)).slice(-2);
  12. if(minutes>=60) {
  13. minutes-=60;
  14. minutes=('0'+minutes).slice(-2);
  15. }
  16. $('#minutes').text(minutes);
  17. let hours = ('0' + Math.floor(tick / 60/60)).slice(-2);
  18. $('#hours').text(hours);
  19. }
  20.  
  21. function myStopFunction() {
  22. clearInterval(myInterval);
  23. clockOn=false;
  24. }
  25.  
  26.  
  27. $('#start-timer').on('click', function () {
  28. if (!clockOn) {
  29. myInterval = setInterval(step, 1000);
  30. }
  31. clockOn = true;
  32. });
  33.  
  34.  
  35.  
  36. $('#stop-timer').on('click', myStopFunction);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement