Advertisement
Guest User

Untitled

a guest
Dec 29th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. var intervalID, currentTimer, milliseconds, seconds, minutes, hours, timerPortfolio = [], timerSet = [];
  2.  
  3. $(document).ready(fillTimeOptions);
  4. $(document).ready(addClickEvents);
  5.  
  6. function Timer(ms, s, m, h){
  7. this.ms = ms;
  8. this.s = s;
  9. this.m = m;
  10. this.h = h;
  11. }
  12.  
  13. function addTimerToSet(t){
  14. var tempTimer = timerSet.slice(0); //creates a shallow copy to prevent altering the original array
  15. tempTimer.push(t);
  16. timerSet = tempTimer;
  17. }
  18.  
  19. function addSetToPortfolio(s){
  20. // The == and != operator consider null equal to only null or undefined
  21. if(s[0] != null){
  22. timerPortfolio.push(s)
  23. }
  24. }
  25.  
  26. function removeTimerFromSet(idx){
  27. var tempTimer = timerSet.slice(0);
  28. tempTimer.splice(idx, 1);
  29. timerSet = tempTimer;
  30. console.log(timerSet);
  31. }
  32.  
  33. function removeSetFromPortfolio(idx){
  34. timerPortfolio.splice(idx, 1);
  35. }
  36.  
  37. function addClickEvents() {
  38. $("#start").on("click", function(){
  39. startCountdown(captureTime());
  40. $("#start").prop("disabled", true);
  41. $("#clear").prop("disabled", false);
  42. $("#pause").prop("disabled", false);
  43. });
  44. $("#clear").on("click", function(){
  45. clearCount();
  46. removeTimerDisplay();
  47. $("#start").prop("disabled", false);
  48. $("#pause").prop("disabled", true);
  49. $("#continue").prop("disabled", true);
  50. });
  51. $("#pause").on("click", function(){
  52. clearCount();
  53. $("#pause").prop("disabled", true);
  54. $("#continue").prop("disabled", false);
  55. });
  56. $("#continue").on("click", function(){
  57. startCountdown(currentTimer);
  58. $("#pause").prop("disabled", false);
  59. $("#continue").prop("disabled", true);
  60. });
  61. $("#addTimer").on("click", function(){
  62. addTimerToSet(captureTime());
  63. fillSet();
  64. });
  65. $("#addSet").on("click", function(){
  66. addSetToPortfolio(timerSet);
  67. fillPortfolio();
  68. clearSet();
  69. });
  70. $("#clearSetDisplay").on("click", function(){
  71. clearSet();
  72. });
  73. $("#clearPortfolioDisplay").on("click", function(){
  74. clearPortfolio();
  75. });
  76. }
  77.  
  78. function startCountdown(t) {
  79. displayCount(t); //first function call displays number immediately instead of with delay
  80. intervalID = setInterval(function (){displayCount(t);}, 10); //10 milliseconds is the safest minimum value for the delay
  81. }
  82.  
  83. function captureTime() {
  84. hours = $("#hours").val();
  85. minutes = $("#minutes").val();
  86. seconds = $("#seconds").val();
  87. milliseconds = 100; //to reach the right amount of milliseconds with the 10ms delay given in setInterval in startCountdown
  88. var timer = new Timer(milliseconds, seconds, minutes, hours);
  89. return timer;
  90. }
  91.  
  92. function displayCount(t){
  93. if(t.h == 0 && t.m == 0 && t.s == 0){
  94. $("#timeDisplay").html(pad(t.h) + ":" + pad(t.m) + ":" + pad(t.s));
  95. clearCount();
  96. }
  97. else {
  98. $("#timeDisplay").html(pad(t.h) + ":" + pad(t.m) + ":" + pad(t.s));
  99. t.ms--;
  100. if(t.ms < 0) {
  101. t.s--;
  102. t.ms += 100;
  103. }
  104. if(t.s < 0) {
  105. t.m--;
  106. t.s += 60;
  107. }
  108. if(t.m < 0) {
  109. t.h--;
  110. t.m += 60;
  111. }
  112. }
  113. currentTimer = t; //keeps current timer for pause/continue due to loss of timer with clearInterval()
  114. }
  115.  
  116. function removeTimer(idx) {
  117. removeTimerFromSet(idx);
  118. fillSet();
  119. }
  120.  
  121. function removeSet(idx) {
  122. removeSetFromPortfolio(idx);
  123. fillPortfolio();
  124. }
  125.  
  126. function removeTimerDisplay() {
  127. $("#timeDisplay").html("");
  128. }
  129.  
  130. function clearCount(){
  131. clearInterval(intervalID);
  132. }
  133.  
  134. function fillTimeOptions(){
  135. for (var i = 0; i < 60; i++) {
  136. document.getElementById("seconds").innerHTML += "<option value='" + i + "'>" + i + "</option>";
  137. document.getElementById("minutes").innerHTML += "<option value='" + i + "'>" + i + "</option>";
  138. };
  139. for (var i = 0; i < 24; i++) {
  140. document.getElementById("hours").innerHTML += "<option value='" + i + "'>" + i + "</option>";
  141. };
  142. }
  143.  
  144. function fillSet(){
  145. $("#myTimers").html("");
  146. for (var i = 0; i < timerSet.length; i++) {
  147. var div = document.createElement("div");
  148. div.innerHTML = "Timer " + (i+1) + ": " + pad(timerSet[i].h) + ":" + pad(timerSet[i].m) + ":" + pad(timerSet[i].s) +
  149. "<input type='button' value='Remove' onclick='removeTimer(" + i + ")'><br>";
  150. $("#myTimers").append(div);
  151. };
  152. }
  153.  
  154. function fillPortfolio(){
  155. $("#mySets").html("");
  156. for (var i = 0; i < timerPortfolio.length; i++) {
  157. var div = document.createElement("div");
  158. div.innerHTML = "Set " + (i+1) + ": " + timerPortfolio[i] +
  159. "<input type='button' value='Remove' onclick='removeSet("+
  160. i +")'><input type='button' value='View Set' onclick='viewSet("+
  161. i +")'><br>";
  162. $("#mySets").append(div);
  163. };
  164. }
  165.  
  166. function clearSet() {
  167. $("#myTimers").html("");
  168. timerSet = [];
  169. }
  170.  
  171. function clearPortfolio() {
  172. $("#mySets").html("");
  173. timerPortfolio = [];
  174. }
  175.  
  176. function viewSet(idx) {
  177. timerSet = timerPortfolio[idx];
  178. fillSet();
  179. }
  180.  
  181. /*Pads variables with a leading zero, to display time in this format: 00:00:00*/
  182. function pad(num) {
  183. var s = "0" + num;
  184. return s.substr(s.length - 2);
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement