countdown.js

main 50 lines · 1.9 KB Raw
Alex Alex Commit Initial commit 01/10/2026 09:20
1function countDown(elid, seconds, msg){
2 // Set the date we're counting down to
3 var countDownDate = new Date();
4 countDownDate.setSeconds(countDownDate.getSeconds() + seconds);
5 var countDownDate = countDownDate.getTime();
6
7 // Update the count down every 1 second
8 window.countDownInterval = setInterval(function() {
9
10 // Get todays date and time
11 var now = new Date().getTime();
12
13 // Find the distance between now and the count down date
14 var distance = countDownDate - now;
15
16 // Time calculations for days, hours, minutes and seconds
17 var days = Math.floor(distance / (1000 * 60 * 60 * 24));
18 var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
19 var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
20 var seconds = Math.floor((distance % (1000 * 60)) / 1000);
21
22 // Display the result in the element with id="demo"
23 document.getElementById(elid).innerHTML = "<b>Server starts in:</b> "+ days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
24
25 // If the count down is finished, write some text
26 if (distance < 0) {
27 clearInterval(window.countDownInterval);
28 document.getElementById(elid).innerHTML = msg;
29 }
30 }, 1000);
31
32 // Get todays date and time
33 var now = new Date().getTime();
34
35 // Find the distance between now and the count down date
36 var distance = countDownDate - now;
37
38 // Time calculations for days, hours, minutes and seconds
39 var days = Math.floor(distance / (1000 * 60 * 60 * 24));
40 var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
41 var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
42 var seconds = Math.floor((distance % (1000 * 60)) / 1000);
43
44 // Display the result in the element with id="demo"
45 document.getElementById(elid).innerHTML = "<b>Server starts in:</b> "+ days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
46
47 if (distance < 0) {
48 document.getElementById(elid).innerHTML = msg;
49 }
50}
Top