From be2bbe35cf49763ad0258c005f3cbdddd7a21d75 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Wed, 13 Jun 2018 16:43:42 +0100 Subject: Compile as much JS with Gulp as possible This will concatenate ALL of our JS, and minify it - thus leaving us with a single file to be loaded. There's a few libraries we can't do this with, unfortunately - these are now added in fouc.js: * Ace Editor * Flatpickr * Font-Awesome --- js/src/countdown.js | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 js/src/countdown.js (limited to 'js/src/countdown.js') diff --git a/js/src/countdown.js b/js/src/countdown.js new file mode 100644 index 00000000..7eaa650c --- /dev/null +++ b/js/src/countdown.js @@ -0,0 +1,79 @@ +"use strict"; + +(function(){ // Use a closure to avoid polluting global scope + // TODO: This needs to be built into the jams system + const startjam = new Date(Date.UTC(2018, 2, 23)); + const endjam = new Date(Date.UTC(2018, 2, 26)); + + const 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": "" + + "
" + + " " + + " Code Jam Countdown" + + " " + + "

...

" + + "(Tap/click to dismiss)" + + "
", + "pos": "bottom-right", + "timeout": endjam - now + } + ); + + const heading = document.getElementById("countdown-title"); + + if (now > startjam.getTime()) { // Jam's already started + heading.innerHTML = "Current code jam ends in..."; + goal = endjam.getTime(); + } else { + heading.innerHTML = "Next code jam starts in..."; + goal = startjam.getTime(); + } + + const refreshCountdown = setInterval(() => { // 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); + } +})(); -- cgit v1.2.3