forked from kokonior/Javascript-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountdown
32 lines (25 loc) · 1.3 KB
/
countdown
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
const second = 1000,
minute = second * 60,
hour = minute * 60,
day = hour * 24,
date = new Date(($_GET.date || '0').replace(/%20/g, ' '))
let countDown = date != 'Invalid Date' ? date.getTime() : new Date(0).getTime(),
timer = {},
x = setInterval(function() {
let now = new Date().getTime(),
distance = countDown - now;
timer.days = Math.floor(distance / (day)) > 0 ? Math.floor(distance / (day)) + " Hari " : '';
timer.hours = Math.floor((distance % (day)) / (hour)) > 0 ? Math.floor((distance % (day)) / (hour)) + " Jam " : '';
timer.minutes = Math.floor((distance % (hour)) / (minute)) > 0 ? Math.floor((distance % (hour)) / (minute)) + " Menit " : '';
timer.seconds = Math.floor((distance % (minute)) / second) > 0 ? Math.floor((distance % (minute)) / second) + " Detik" : '';
document.getElementById('timer').innerText = timer.days + timer.hours + timer.minutes + timer.seconds;
//do something later when date is reached
if (distance <= 0) {
clearInterval(x);
ondate = true;
document.getElementById('countdown').innerHTML = '<h1>Selesai</h1>';
}
}, second)
if (countDown - new Date().getTime() > 0) {
document.getElementById('countdown').style.display = 'block'
}