-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathCountdown Timer.js
More file actions
41 lines (21 loc) · 949 Bytes
/
Countdown Timer.js
File metadata and controls
41 lines (21 loc) · 949 Bytes
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
// time to countdown from (in milliseconds)
let countDownDate = new Date().getTime() + 24 * 60 * 60 * 1000;
// countdown timer
let x = setInterval(function() {
// get today's date and time in milliseconds
let now = new Date().getTime();
// find the interval between now and the countdown time
let timeLeft = countDownDate - now;
// time calculations for days, hours, minutes and seconds
const days = Math.floor( timeLeft/(1000*60*60*24) );
const hours = Math.floor( (timeLeft/(1000*60*60)) % 24 );
const minutes = Math.floor( (timeLeft/1000/60) % 60 );
const seconds = Math.floor( (timeLeft/1000) % 60 );
// display the result in the element with id="demo"
console.log(days + "d " + hours + "h " + minutes + "m " + seconds + "s ");
// clearing countdown when complete
if (timeLeft < 0) {
clearInterval(x);
console.log('CountDown Finished');
}
}, 2000);