support multiple dates

master
lub 2 days ago
parent 39fdf44612
commit e50db84629

@ -13,26 +13,36 @@ const countdown = (target, heading) => {
timeleft.minutes = Math.trunc(diff%day%hour/minute); timeleft.minutes = Math.trunc(diff%day%hour/minute);
timeleft.seconds = Math.trunc(diff%day%hour%minute/second); timeleft.seconds = Math.trunc(diff%day%hour%minute/second);
const formatted = Object.keys(timeleft).map((unit) => { const formatted = Object.keys(timeleft).map(unit => {
// retrieve value, cast it to string and optionally pad it with a leading 0 // retrieve value, cast it to string and optionally pad it with a leading 0
return timeleft[unit] return timeleft[unit]
.toString() .toString()
.padStart(2, '0'); .padStart(2, '0');
}); });
return ` if (heading === 1 || formatted.filter(timespan => timespan.substr(0, 1) == '-').length === 0) {
return `
<h${heading}>${formatted[0]} Days</h${heading}> <h${heading}>${formatted[0]} Days</h${heading}>
<h${heading}>${formatted[1]} Hours</h${heading}> <h${heading}>${formatted[1]} Hours</h${heading}>
<h${heading}>${formatted[2]} Minutes</h${heading}> <h${heading}>${formatted[2]} Minutes</h${heading}>
<h${heading}>${formatted[3]} Seconds</h${heading}> <h${heading}>${formatted[3]} Seconds</h${heading}>
`; `;
}
}; };
const hash_date = new Date(location.hash.substr(1)) const hash_date = new Date(location.hash.substr(1));
const update_all = () => { const update_all = () => {
let i = 1 let i = 0;
const date_list = location.hash.split('#').slice(1).map((fragment) => {return new Date(fragment)}) const curDate = new Date();
document.getElementById('output').innerHTML = countdown(date_list[0], i);
document.body.innerHTML = location
.hash
.split('#')
.slice(1)
.map(fragment => new Date(fragment))
.sort((a, b) => b - a)
.map(date => countdown(date, ++i))
.join('');
} }
update_all(); update_all();
setInterval(update_all, 500); setInterval(update_all, 500);

Loading…
Cancel
Save