aboutsummaryrefslogtreecommitdiffstats
path: root/static/js/countdown.js
blob: be4a11c9db65df3a8d634d9eea2cba17f1c8bae3 (plain) (blame)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"use strict";

(function(){  // Use a closure to avoid polluting global scope
    const startjam = new Date(Date.UTC(2018, 2, 23));
    const endjam = new Date(Date.UTC(2018, 2, 26));

    let now = Date.now();
    let goal;

    if (now+1000 < endjam.getTime()) {  // Only do anything if the jam hasn't ended
        UIkit.notification(  // Spawn the notification
            {
                message: ""
                  + "<div class='uk-text-center'>"
                  + "    <span id=\"countdown-title\" class=\"uk-text-center\">"
                  + "        <a href=\"/info/jams\">Code Jam</a> Countdown"
                  + "    </span>"
                  + "    <p class='uk-text-large' id=\"countdown-remaining\">...</p>"
                  + "<small style='font-size: 0.6em;'>(Tap/click to dismiss)</small>"
                  + "</div>",
                pos: "bottom-right",
                timeout: endjam - now
            }
        );

        const heading = document.getElementById("countdown-title");

        if (now > startjam.getTime()) {  // Jam's already started
            heading.innerHTML = "Current <a href=\"/info/jams\">code jam</a> ends in...";
            goal = endjam.getTime();
        } else {
            heading.innerHTML = "Next <a href=\"/info/jams\">code jam</a> starts in...";
            goal = startjam.getTime();
        }

        let refreshCountdown = setInterval(function() {  // Create a repeating task
            let delta = goal - Date.now();  // Time until the goal is met

            if (delta <= 1000) {  // Goal has been met, best reload
                clearInterval(refreshCountdown);
                return location.reload();
            }

            let days = Math.floor(delta / (24*60*60*1000));
            delta -= days * (24*60*60*1000);

            let hours = Math.floor(delta / (60*60*1000));
            delta -= hours * (60*60*1000);

            let minutes = Math.floor(delta / (60*1000));
            delta -= minutes * (60*1000);

            let seconds = Math.floor(delta / 1000);

            if (days < 10) {
                days = "0"+days;
            }

            if (hours < 10) {
                hours = "0"+hours;
            }

            if (minutes < 10) {
                minutes = "0"+minutes;
            }

            if (seconds < 10) {
                seconds = "0"+seconds;
            }

            try {
                document.getElementById('countdown-remaining').innerHTML = `${days}:${hours}:${minutes}:${seconds}`;
            } catch (e) {  // Notification was probably closed, so we can stop counting
                return clearInterval(refreshCountdown);
            }
        }, 500);
    }
}());