server/assets/development/scripts/countdown.js

53 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

const countdown = document.getElementById('countdown')
const counter = document.getElementById('counter')
const daysContainer = document.getElementById('days')
const hoursContainer = document.getElementById('hours')
const minutesContainer = document.getElementById('minutes')
const secondsContainer = document.getElementById('seconds')
let shown = false
//const enableReload = window.location.pathname.indexOf('c') === -1
enableReload = true
function render() {
let current = new Date()
let difference = openDate - current
var _second = 1000,
_minute = _second * 60,
_hour = _minute * 60,
_day = _hour * 24
var days = Math.floor(difference / _day),
hours = Math.floor((difference % _day) / _hour),
minutes = Math.floor((difference % _hour) / _minute),
seconds = Math.floor((difference % _minute) / _second)
days = (String(days).length >= 2) ? days : '0' + days
hours = (String(hours).length >= 2) ? hours : '0' + hours
minutes = (String(minutes).length >= 2) ? minutes : '0' + minutes
seconds = (String(seconds).length >= 2) ? seconds : '0' + seconds
if (openDate <= current && enableReload) {
console.log('FINISHED!')
window.location = '/'
} else {
daysContainer.textContent = days
hoursContainer.textContent = hours
minutesContainer.textContent = minutes
secondsContainer.textContent = seconds
}
if (!shown) {
setTimeout(() => {
countdown.style.opacity = 1
shown = true
}, 300)
}
}
render()
setInterval(render, 1000)