summaryrefslogtreecommitdiff
path: root/views/http/components/timer.twig
blob: 4898a2e6b6a0f3231dcd9ba8e2ac53238d734e73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<span class="timer" data-time="{{ time }}">
    <span class="timer__time"></span>
</span>
<script>
document.addEventListener('DOMContentLoaded', function (ev) {
  const timer = document.querySelector('.timer[data-time="{{ time }}"] .timer__time');
  const time = new Date('{{ time }}');

  const interval = setInterval(setTime, 1000);
  function setTime() {
    let diff = time - new Date();
    if (diff <= 0) {
      clearInterval(interval);
      window.location.reload();
    }

    const dd = Math.floor(diff/1000/60/60/24);
    diff -= dd*1000*60*60*24;
    const hh = Math.floor(diff/1000/60/60);
    diff -= hh*1000*60*60;
    const mm = Math.floor(diff/1000/60);
    diff -= mm*1000*60;
    const ss = Math.floor(diff/1000);

    timer.innerHTML = `${('00' + hh).slice(-2)}:${('00' + mm).slice(-2)}:${('00' + ss).slice(-2)}`;
    if (dd > 0) {
      timer.innerHTML = `${('00' + dd).slice(-2)}:${timer.innerHTML}`;
    }
  }
  setTime();
});
</script>