-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebinar-schedule-v5.0.js
157 lines (133 loc) · 5.87 KB
/
webinar-schedule-v5.0.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const timerState = {
currentDate: '',
nextDate: '',
currentDateSec: '',
nextDateSec: '',
};
function nextWebinar(currentDate, currentWebTime){
let nextWebinarDate = '';
for (let idx = 0; idx < webinarSchedule.length; idx++) {
if (webinarSchedule[idx]) {
const currentDateWeb = Date.parse(new Date(`${webinarSchedule[idx].date}, ${webinarSchedule[idx].time}`));
if (currentDateWeb > currentWebTime) {
//console.log('Webinar Date', webinarSchedule[idx].date, webinarSchedule[idx].time);
nextWebinarDate = `${webinarSchedule[idx].date}, ${webinarSchedule[idx].time}`;
break;
}
}
}
//console.log(nextWebinarDate);
return nextWebinarDate;
};
function initStates(){
timerState.currentDate = new Date().toLocaleString('en-US', { timeZone: 'America/New_York' });
timerState.currentDateSec = Date.parse(timerState.currentDate);
timerState.nextDate = nextWebinar(timerState.currentDate.split(',')[0], timerState.currentDateSec);
if (timerState.nextDate !== '') {
timerState.nextDateSec = Date.parse(timerState.nextDate);
}
};
function unitCount(unit){
const toTens = () => String(Number.parseInt(unit / 10));
const toOnes = () => String(unit % 10);
return toTens() + toOnes();
};
function unitaryCountHandler(){
if (parseInt(document.querySelector('.webinar__timer--days > .webinar__timer--count').textContent) <= 1) {
document.querySelectorAll('.webinar__timer--days > .webinar__timer--label').forEach((timer) => {
timer.textContent = 'Day';
});
}
if (parseInt(document.querySelector('.webinar__timer--hours > .webinar__timer--count').textContent) <= 1) {
document.querySelectorAll('.webinar__timer--hours > .webinar__timer--label').forEach((timer) => {
timer.textContent = 'Hr';
});
}
if (parseInt(document.querySelector('.webinar__timer--mins > .webinar__timer--count').textContent) <= 1) {
document.querySelectorAll('.webinar__timer--mins > .webinar__timer--label').forEach((timer) => {
timer.textContent = 'Min';
});
}
if (parseInt(document.querySelector('.webinar__timer--secs > .webinar__timer--count').textContent) <= 1) {
document.querySelectorAll('.webinar__timer--secs > .webinar__timer--label').forEach((timer) => {
timer.textContent = 'Sec';
});
}
};
function updateTimerUI(day, hrs, min, sec){
document.querySelectorAll('.webinar__timer--days > .webinar__timer--count').forEach((timer) => {
timer.textContent = day;
});
document.querySelectorAll('.webinar__timer--hours > .webinar__timer--count').forEach((timer) => {
timer.textContent = hrs;
});
document.querySelectorAll('.webinar__timer--mins > .webinar__timer--count').forEach((timer) => {
timer.textContent = min;
});
document.querySelectorAll('.webinar__timer--secs > .webinar__timer--count').forEach((timer) => {
timer.textContent = sec;
});
unitaryCountHandler();
};
function TimerHandler(){
// initialize states
initStates();
// start timer
const webinarTimer = setInterval(() => {
if (timerState.nextDate === '') {
clearInterval(webinarTimer);
}
timerState.currentDate = new Date().toLocaleString('en-US', { timeZone: 'America/New_York' });
timerState.currentDateSec = Date.parse(timerState.currentDate);
const distanceCount = timerState.nextDateSec - timerState.currentDateSec;
if (timerState.nextDateSec == "" || timerState.nextDateSec == null){
if(document.querySelector('.webinar__nav-timer') != null){
document.querySelector('.webinar__nav-timer').style.display = 'none';
}
if(document.querySelector('.webinar__timer') != null){
document.querySelector('.webinar__timer').style.display = 'none';
}
$('.webinar__timer-nav').css("display","none");
$('.webinar__timer').css("display","none");
return;
}
//console.log("nextDateSec: "+timerState.nextDateSec);
//console.log("distanceCount: "+distanceCount);
const day = Math.floor(distanceCount / (1000 * 60 * 60 * 24));
const hrs = Math.floor((distanceCount % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const min = Math.floor((distanceCount % (1000 * 60 * 60)) / (1000 * 60));
const sec = Math.floor((distanceCount % (1000 * 60)) / 1000);
// separating into tens and ones
const dayCount = unitCount(day);
const hrCount = unitCount(hrs);
const minCount = unitCount(min);
const secCount = unitCount(sec);
// update UI
updateTimerUI(dayCount, hrCount, minCount, secCount);
if (distanceCount <= 0) {
// move timer to next date if reached 0
initStates();
if (timerState.nextDate === '') {
clearInterval(webinarTimer);
document.querySelectorAll('.webinar__timer').forEach((timer) => {
timer.classList.add('is-hidden');
});
}
}
}, 1000);
};
$( document ).ready(function() {
TimerHandler();
});
// scroll function which displays the timer in the sticky header
// below element selectors will only run for the home page.
const stickyTimerHandler = () => {
window.onscroll = () => {
if(document.querySelector('.webinar__nav-timer') == null) return;
if (scrollY > document.querySelector('#numberRoller').offsetTop - 140) {
document.querySelector('.webinar__nav-timer').style.display = 'flex';
} else {
document.querySelector('.webinar__nav-timer').style.display = 'none';
}
};
};