Guest User

Untitled

a guest
Jun 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. // create window.TIMER
  2. window.TIMER = window.TIMER || {};
  3.  
  4. // begin init function
  5. var init = function() {
  6. var activeTime = 0; // number of seconds active timer should run for
  7. var active_timer_is_on = false; // flag for active timer
  8. var intervals_left=0; //intializing interval counter
  9. var number_of_intervals = 0; // number of intervals the workout timer should perform.
  10. var nextPhase = ""; // number of intervals remaining while the timer is running.
  11. var restTime = 0; // number of seconds rest timer should run fo
  12. var rest_timer_is_on = false; // flag for rest time
  13. TIMER.time_left = 0; //intialize timer counter
  14. var timer_is_on = false; // flag for workout timer
  15. var wt; // initializing timeout variable
  16.  
  17. // function to switch phases mid interval
  18. var switchPhase = function(){
  19. if (active_timer_is_on === true){
  20. active_timer_is_on = false;
  21. rest_timer_is_on = true;
  22. return;
  23. }
  24. if (rest_timer_is_on === true){
  25. rest_timer_is_on = false;
  26. active_timer_is_on = true;
  27. } else {
  28. console.log('both timers in "switchPhase" are false')
  29. }
  30. };
  31.  
  32. // function to output how much time and intervals are left in timer
  33. // console.log is for debugging purposes and will be removed from final version
  34. var msgGenerator = function(time, phase, intervals){
  35. if (intervals > 1){
  36. console.log(time + " seconds left until the next " + phase + " phase and you have " + intervals + " intervals left.");
  37. return time + "<br/>" + intervals + " intervals left.";
  38. }
  39. if (intervals === 1){
  40. console.log (time + " seconds left until the next " + phase + " phase and you have " + intervals + " interval left.");
  41. return time + "<br/>" + intervals + " interval left.";
  42. }
  43. if (intervals === 0 && phase === "rest"){
  44. console.log (time + " seconds left in this active phase and this is the last interval.");
  45. return time + "<br /> Last active phase. Long rest after this. You can do it!";
  46. }
  47. if (intervals === 0 && phase === "active"){
  48. console.log (time + " seconds left until you are done!");
  49. return time + "<br /> The last rest phase! You are almost done!";
  50. }
  51. if (intervals < 0){
  52. console.log ("YOUR ARE DONE!");
  53. return "FINISHED";
  54. }
  55. console.log ("no msg generated " + intervals + " intervals left");
  56. return;
  57. };
  58.  
  59. // function that actually counts down the time
  60. TIMER.timedCount = function(time_left) {
  61. if (time_left >= 0){
  62. document.getElementById('counter').innerHTML = msgGenerator(time_left, nextPhase, intervals_left);
  63. time_left--;
  64. wt=setTimeout("TIMER.timedCount("+time_left+")",1000);
  65. } else {
  66. clearTimeout(wt);
  67. switchPhase();
  68. fartlekCounter();
  69. }
  70. }
  71.  
  72. // checks interval count
  73. var fartlekCounter = function() {
  74.  
  75. if (intervals_left > 0 && active_timer_is_on === true){
  76. //time_left = activeTime;
  77. nextPhase = "rest";
  78. intervals_left--;
  79. document.getElementById('counter').style.background = "green";
  80. TIMER.timedCount(activeTime);
  81. } else if (intervals_left >= 0 && rest_timer_is_on === true){
  82. //time_left = restTime;
  83. nextPhase = "active";
  84. document.getElementById('counter').style.background = "red";
  85. TIMER.timedCount(restTime);
  86. } else{
  87. timer_is_on = false;
  88. document.getElementById('counter').innerHTML = msgGenerator(intervals_left);
  89. console.log("YOU ARE DONE!");
  90. }
  91. };
  92.  
  93. //starts the timer
  94. TIMER.doTimer = function () {
  95. if (timer_is_on === true){
  96. return;
  97. }
  98. activeTime = document.getElementById('active_time').value;
  99. number_of_intervals = document.getElementById('number_of_intervals').value;
  100. restTime = document.getElementById('rest_time').value;
  101. timer_is_on = true;
  102. active_timer_is_on = true;
  103. intervals_left = number_of_intervals;
  104. fartlekCounter();
  105. }
  106.  
  107. //Stop Timer
  108. TIMER.stopCount = function (){
  109. clearTimeout(wt);
  110. timer_is_on = false;
  111. }
  112. // end init function
  113. };
  114.  
  115. $(document).ready(init);
  116.  
  117. //var add_event_handlers = function() {
  118. // $('#start_btn').on('click', TIMER.doTimer);
  119. //};
Add Comment
Please, Sign In to add comment