Advertisement
YavorJS

clearInterval problem

Jun 25th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Timer</title>
  6. <script src="https://code.jquery.com/jquery-3.1.0.min.js"
  7. integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s="
  8. crossorigin="anonymous"></script>
  9. <style>
  10. #timer {
  11. font-size: 5em;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="timer">
  17. <span id="hours" class="timer">00</span>:
  18. <span id="minutes" class="timer">00</span>:
  19. <span id="seconds" class="timer">00</span>
  20. <button id="start-timer">Start</button>
  21. <button id="stop-timer">Stop</button>
  22. </div>
  23. <!--<script src="timer.js"></script>-->
  24. <script>
  25. window.onload = function () {
  26. timer();
  27. }
  28. </script>
  29. <script>
  30.  
  31. function timer() {
  32. let tick = 0;
  33. let clockOn=false;
  34.  
  35. let myInterval = function () {
  36. if(clockOn===false){
  37. setInterval(step, 1000);
  38. }
  39. else{
  40. return;
  41. }
  42. clockOn=true;
  43. };
  44.  
  45.  
  46. function step() {
  47. tick++;
  48. let seconds = ('0' + tick % 60).slice(-2);
  49. $('#seconds').text(seconds);
  50. let minutes = ('0' + Math.floor(tick / 60)).slice(-2);
  51. $('#minutes').text(minutes);
  52. let hours = ('0' + Math.floor(minutes / 60)).slice(-2);
  53. $('#hours').text(hours);
  54. }
  55.  
  56. function myStopFunction() {
  57. clearInterval(myInterval);
  58. console.log('clear');
  59. }
  60.  
  61.  
  62. $('#start-timer').on('click', myInterval);
  63.  
  64.  
  65. $('#stop-timer').on('click', myStopFunction);
  66. }
  67.  
  68.  
  69. </script>
  70. </body>
  71. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement