Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. var timer;
  2.  
  3. $(document).ready(function(){
  4. var working = false;
  5. var active = '';
  6. var workTime = 0;
  7. var breakTime = 0;
  8. checkStatus();
  9.  
  10. //Test Vars
  11.  
  12. /* Alternate Method of 'checkStatus Function'
  13.  
  14. working ? $('#pause').addClass('disabled')&& $('#start').removeClass('disabled'): $('#start').addClass('disabled')&& $('#pause').removeClass('disabled');
  15. */
  16.  
  17. //Controls if a button is disabled based on status of timer
  18. function checkStatus() {
  19. if (!working) {
  20. $('#start').removeClass('disabled');
  21. $('#pause').addClass('disabled');
  22. $('#reset').addClass('disabled');
  23. } else {
  24. $('#pause').removeClass('disabled');
  25. $('#reset').removeClass('disabled');
  26. $('#start').addClass('disabled');
  27. }
  28. }
  29. //End Check Status
  30. //Function to Show Time !!Finished!!
  31. function showTime(time) {
  32. var min = Math.floor(time/60);
  33. var sec = Math.round(time%60);
  34. if (sec < 10) {
  35. sec = '0' + sec
  36. }
  37. var timeString = min+':'+sec
  38. $('#msg').text("Hey! You've only got "+timeString+" left to go!")
  39. }
  40. //End showTime
  41. //Enables the timer !!Mostly Finished!!
  42. function startTimer() {
  43. $('.jumbotron').css('visibility', 'visible');
  44. return setInterval(function() {
  45. console.log("Work Timer...")
  46. workTime--;
  47. if (workTime < 0) {
  48. clearInterval(timer);
  49. timer = breakTimer();
  50. } else {
  51. showTime(workTime);
  52. }
  53. }, 1000);
  54. }
  55. //End Timer
  56. //What Happens when #start is pressed
  57. function start() {
  58. if (working == true){ //This keeps it from being spammable
  59. return
  60. } //Else
  61. workTime = $('#work').val()*60;
  62. breakTime = $('#break').val()*60;
  63. working = true;
  64. checkStatus();
  65. timer = startTimer();
  66. }
  67.  
  68.  
  69. //What Happens when #pause/resume is pressed
  70. function pause() {
  71. clearInterval(timer);
  72. $('.resume').unbind().click(resume);
  73. $('#pause').html('Resume');
  74. $('#pause').addClass('resume');
  75. $('#pause').removeClass('pause');
  76. $('.resume').click(resume);
  77. }
  78.  
  79.  
  80. function resume(){
  81. $('#pause').unbind().click(pause);
  82. $('#pause').html('Pause');
  83. $('#pause').addClass('pause');
  84. $('#pause').removeClass('resume');
  85. timer = startTimer();
  86. }
  87. //What happens when #reset is pressed
  88. function reset() {
  89. clearInterval(timer);
  90. working = false;
  91. workTime = 0;
  92. breakTime = 0;
  93. checkStatus();
  94. $('.jumbotron').css('visibility', 'hidden');
  95. $('#msg').html("");
  96. }
  97. //Break Timer
  98. function breakTimer() {
  99. $('.jumbotron').css('visibility', 'visible');
  100. return setInterval(function() {
  101. console.log("Break Timer...");
  102. breakTime--;
  103. if (breakTime < 0) {
  104. clearInterval(timer);
  105. working = false;
  106. start();
  107. } else {
  108. showTime(breakTime);
  109. }
  110. }, 1000);
  111. }
  112. //Button Association
  113. $('#start').click(start);
  114. $('#work').keypress(function(e) {
  115. if(e.which == 13) {
  116. start();
  117. }
  118. });
  119. //This Makes Enter Work as well to Start
  120. $('.pause').click(pause);
  121. $('#reset').click(reset);
  122.  
  123. }); //End of DocReady
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement