Advertisement
Guest User

newJSfix

a guest
Mar 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. function addZero(i) {
  2. if (i < 10) {
  3. i = "0" + i;
  4. }
  5. return i;
  6. }
  7.  
  8. function curTime() {
  9. var d = new Date();
  10. var x = document.getElementById("time");
  11. var h = addZero(d.getHours());
  12. var m = addZero(d.getMinutes());
  13. var s = addZero(d.getSeconds());
  14. x.innerHTML = h + "." + m;
  15. }
  16.  
  17. var closest = function(a, x) {
  18. var lo, hi;
  19. for (var i = a.length; i--;) {
  20. if (a[i] <= x && (lo === undefined || lo < a[i])) lo = a[i];
  21. if (a[i] >= x && (hi === undefined || hi > a[i])) hi = a[i];
  22. };
  23. return [lo, hi];
  24. }
  25.  
  26. var toHHMM = (secs) => {
  27. var sec_num = parseInt(secs, 10)
  28. var hours = Math.floor(sec_num / 3600) % 24
  29. var minutes = Math.floor(sec_num / 60) % 60
  30. return [hours,minutes]
  31. .map(v => v < 10 ? "0" + v : v)
  32. .filter((v,i) => v !== "00" || i > 0)
  33. .join(":")
  34. }
  35.  
  36. function includes(k) {
  37. for(var i=0; i < this.length; i++){
  38. if( this[i] === k || ( this[i] !== this[i] && k !== k ) ){
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44.  
  45. var timeTable = Array();
  46.  
  47. window.onload = function () {
  48. curTime();
  49.  
  50. var days = ['Søndag', 'Mandag', 'Tirsdag', 'Onsdag', 'Torsdag', 'Fredag', 'Lørdag'];
  51. var months = ['Januar', 'Februar', 'Marts', 'April', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'December'];
  52. var curDates = new Date();
  53. var dayName = days[curDates.getDay()];
  54. var dayDate = curDates.getDate();
  55. var dayMonth = months[curDates.getMonth()];
  56.  
  57. $("#date").html(dayName + " d. " + dayDate + " " + dayMonth);
  58.  
  59. $("table tr").each(function(i){
  60. var currentSelector = $(this);
  61.  
  62. var timeFrom = currentSelector.attr("timeFrom");
  63. var timeTo = currentSelector.attr("timeTo");
  64.  
  65. var timeFromVal = timeFrom.split(':');
  66. var timeToVal = timeTo.split(':');
  67.  
  68. var timeFromSec = (+timeFromVal[0]) * 60 * 60 + (+timeFromVal[1]) * 60;
  69. var timeToSec = (+timeToVal[0]) * 60 * 60 + (+timeToVal[1]) * 60;
  70.  
  71. timeTable.includes = includes;
  72.  
  73. if (timeTable.includes(timeFromSec)) {
  74. // Already Exists
  75. } else {
  76. timeTable.push(timeFromSec);
  77. }
  78. if (timeTable.includes(timeToSec)) {
  79. // Already Exists
  80. } else {
  81. timeTable.push(timeToSec);
  82. }
  83. })
  84. lastTime = timeTable.slice(-1)[0];
  85.  
  86. var curDate = new Date();
  87. var hours = curDate.getHours();
  88. var mins = curDate.getMinutes();
  89. currentTime = (hours * 60 * 60) + (mins * 60);
  90.  
  91. minMax = closest(timeTable, currentTime);
  92.  
  93. curFrom = minMax[0];
  94. curTo = minMax[1];
  95. $("table tr").each(function(i){
  96. if ($(this).attr("timeFrom") == toHHMM(curFrom) && $(this).attr("timeTo") == toHHMM(curTo) || $(this).attr("timeFrom") == toHHMM(currentTime) && curTo) {
  97. $(this).addClass("active");
  98. } else {
  99. $(this).removeClass("active");
  100. }
  101. });
  102. if (!curFrom) {
  103. status2 = "Første time begynger kl. " + toHHMM(curTo);
  104. $(".split > div:first-child").removeClass("hide");
  105. } else if (!curTo || currentTime == lastTime) {
  106. status2 = "Skolen er slut for i dag";
  107. $(".split > div:first-child").addClass("hide");
  108. } else if (!$("*").hasClass("active")) {
  109. status2 = "Der er frikvarter - Næste lektion starter kl. " + toHHMM(curTo);
  110. $(".split > div:first-child").removeClass("hide");
  111. } else {
  112. status2 = "I Skole";
  113. $(".split > div:first-child").removeClass("hide");
  114. }
  115. if (status2 == "I Skole") {
  116.  
  117. percentage = (currentTime - curFrom) / (curTo - curFrom) * 100;
  118. if (currentTime == curFrom) {
  119. percentage = 0;
  120. }
  121. $("#progress").css("width", percentage + "%");
  122. $("#progress").html(status2).removeClass("progressInactive");
  123. } else {
  124. $("#progress").html(status2).addClass("progressInactive");
  125. }
  126. }
  127. window.setInterval(function(){
  128. curTime();
  129.  
  130. var curDate = new Date();
  131. var hours = curDate.getHours();
  132. var mins = curDate.getMinutes();
  133. currentTime = (hours * 60 * 60) + (mins * 60);
  134.  
  135. minMax = closest(timeTable, currentTime);
  136.  
  137. curFrom = minMax[0];
  138. curTo = minMax[1];
  139. $("table tr").each(function(i){
  140. if ($(this).attr("timeFrom") == toHHMM(curFrom) && $(this).attr("timeTo") == toHHMM(curTo) || $(this).attr("timeFrom") == toHHMM(currentTime) && curTo) {
  141. $(this).addClass("active");
  142. } else {
  143. $(this).removeClass("active");
  144. }
  145. });
  146. if (!curFrom) {
  147. status2 = "Første time begynger kl. " + toHHMM(curTo);
  148. $(".split > div:first-child").removeClass("hide");
  149. } else if (!curTo || currentTime == lastTime) {
  150. status2 = "Skolen er slut for i dag";
  151. $(".split > div:first-child").addClass("hide");
  152. } else if (!$("*").hasClass("active")) {
  153. status2 = "Der er frikvarter - Næste lektion starter kl. " + toHHMM(curTo);
  154. $(".split > div:first-child").removeClass("hide");
  155. } else {
  156. status2 = "I Skole";
  157. $(".split > div:first-child").removeClass("hide");
  158. }
  159. if (status2 == "I Skole") {
  160.  
  161. percentage = (currentTime - curFrom) / (curTo - curFrom) * 100;
  162. if (currentTime == curFrom) {
  163. percentage = 0;
  164. }
  165. $("#progress").css("width", percentage + "%");
  166. $("#progress").html(status2).removeClass("progressInactive");
  167. } else {
  168. $("#progress").html(status2).addClass("progressInactive");
  169. }
  170. }, 1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement