You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.4 KiB

  1. document.addEventListener("DOMContentLoaded", function(event) {
  2. updateClock();
  3. setInterval(updateClock, 1000);
  4. });
  5. function getNextOpen(now) {
  6. var days = [[0, 14], [4, 18], [8, 22], [12], [2, 16], [6, 20], [10]]
  7. var nowday = now.getUTCDay();
  8. var nour = now.getUTCHours();
  9. var open_hour = -1;
  10. var d = 0;
  11. while (open_hour == -1) {
  12. var times = days[(nowday + d) % 7];
  13. for (var i = 0; i < times.length; ++i) {
  14. var time = times[i];
  15. if (time == nour) {
  16. return "refresh";
  17. } else if (time > nour || d > 0) {
  18. open_hour = time;
  19. break;
  20. }
  21. }
  22. if (open_hour == -1) {
  23. d += 1;
  24. nour = -1;
  25. }
  26. }
  27. var open = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() + d));
  28. var ts = open.setUTCHours(open_hour);
  29. return open;
  30. }
  31. function updateClock() {
  32. var clock = document.querySelector(".closed-registrations-message .clock");
  33. var now = new Date();
  34. var open = getNextOpen(now);
  35. if (open == "refresh") {
  36. location.reload();
  37. return;
  38. }
  39. var until = open - now;
  40. var ms = until % 1000;
  41. var s = Math.floor((until / 1000)) % 60;
  42. var m = Math.floor((until / 1000 / 60)) % 60;
  43. var h = Math.floor((until / 1000 / 60 / 60));
  44. if (m < 10) m = "0" + m;
  45. if (s < 10) s = "0" + s;
  46. clock.innerHTML = h + ":" + m + ":" + s;
  47. }