-
Notifications
You must be signed in to change notification settings - Fork 1
/
countdown-lite.js
107 lines (93 loc) · 3.01 KB
/
countdown-lite.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict';
if (document.querySelector('.countdown')) {
var endtime = document.querySelector('.countdown').getAttribute('data-time');
// Convert to standard 09, 08, ... 03 instead of 9, 8, ... 3
function makeCorrectDate(uncorrectDate) {
let correctDate = uncorrectDate;
if (uncorrectDate < 10) {
correctDate = '0' + uncorrectDate;
}
return correctDate;
}
// How much time is left
function getDateRemaining(endtime) {
// total = the remaining time
var total = Date.parse(endtime) - Date.now();
var seconds = Math.floor((total / 1000) % 60);
var minutes = Math.floor((total / 1000 / 60) % 60);
var hours = Math.floor((total / (1000 * 60 * 60)) % 24);
var days = Math.floor(total / (1000 * 60 * 60 * 24));
// return objects
return {
'total': total,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}
// Timer initialization
function countdownLite() {
let timer = document.querySelector('.countdown');
let days = timer.querySelector('.countdown__days'),
hours = timer.querySelector('.countdown__hours'),
minutes = timer.querySelector('.countdown__minutes'),
seconds = timer.querySelector('.countdown__seconds');
// timer update every 1000 ms
let timeInterval = setInterval(update, 1000);
function update() {
// getDateRemaining function's result
let total = getDateRemaining(endtime);
// zero check
var nowdate = Date.now();
if (nowdate <= Date.parse(endtime)) {
var nowdate = Date.now();
days.textContent = makeCorrectDate(total.days);
hours.textContent = makeCorrectDate(total.hours);
minutes.textContent = makeCorrectDate(total.minutes);
seconds.textContent = makeCorrectDate(total.seconds);
} else {
days.textContent = 0;
hours.textContent = 0;
minutes.textContent = 0;
seconds.textContent = 0;
}
// Endings of days
switch (total.days) {
case 1:
correctHours = "Day";
break;
default:
correctHours = "Days";
}
document.querySelector('.countdown__days-text').textContent = correctHours;
// Endings of hours
switch (total.hours) {
case 1:
correctHours = "Hour";
break;
default:
correctHours = "Hours";
}
document.querySelector('.countdown__hours-text').textContent = correctHours;
// Endings of minutes
switch (total.minutes) {
case 1:
correctMinutes = "Minute";
break;
default:
correctMinutes = "Minutes";
}
document.querySelector('.countdown__minutes-text').textContent = correctMinutes;
// Endings of seconds
switch (total.seconds) {
case 1:
correctSeconds = "Second";
break;
default:
correctSeconds = "Seconds";
}
document.querySelector('.countdown__seconds-text').textContent = correctSeconds;
}
}
}