server/assets/development/scripts/countdown.js

47 lines
1.4 KiB
JavaScript
Raw 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 target = new Date("2020-09-04 15:00:00")
let shown = false
function render() {
let current = new Date()
let difference = target - 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;
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)