-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (46 loc) · 1.56 KB
/
index.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
class CountdownTimer{
constructor({selector,targetDate}){
this.selector = selector
this.targetDate=targetDate
this.refs = {
days: document.querySelector('[data-value="days"]'),
hours: document.querySelector('[data-value="hours"]'),
mins: document.querySelector('[data-value="mins"]'),
secs: document.querySelector('[data-value="secs"]'),
}
this.start()
}
calcTime(time){
const days = this.pad(Math.floor(time / (1000 * 60 * 60 * 24)));
const hours = this.pad(Math.floor((time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)));
const mins = this.pad(Math.floor((time % (1000 * 60 * 60)) / (1000 * 60)));
const secs = this.pad(Math.floor((time % (1000 * 60)) / 1000));
this.changeDateElements(days,hours,mins,secs)
}
pad (value){
return String(value).padStart(2,'0')
}
changeDateElements(days,hours,mins,secs){
this.refs.days.innerHTML=days
this.refs.hours.innerHTML=hours
this.refs.mins.innerHTML=mins
this.refs.secs.innerHTML=secs
}
changeDate(){
const time=this.targetDate.getTime()-new Date().getTime()
time>0 ? this.calcTime(time) : this.viewError()
}
start(){
this.changeDate()
setInterval(()=>{
this.changeDate()
},1000)
}
viewError(){
document.querySelector(this.selector).innerHTML='Time is over'
}
}
new CountdownTimer({
selector: '#timer-1',
targetDate: new Date('Jul 20, 2021'),
});