Code countdown tự động lặp lại giúp chốt sale

Vừa rồi khách hàng của mình yêu cầu code countdown tự động lặp lại sau 24h, không cần vào set lại thời gian mà nó sẽ tự động lặp lại thời gian sau khi hết 24h, mình thấy hay quá nên share cho anh em luôn
yêu cầu khách hàng code countdown tự động lặp lại thời gian
Ưu điểm của code countdown tự động lặp lại
- Giúp chốt sale hiệu quả hơn
- tự động lặp lại thời gian ( không cần phải vào set lại thời gian )
- Dễ dàng chỉnh sửa đối với lập trình viên
Chia sẻ code countdown tự động lặp lại
- Dưới đây là code mình đang để dạng bình thường, ai cần chỉnh lại cho đẹp hay như nào để hợp theo ý của bạn thì tự sửa nha
- bạn chỉ cần bỏ vào file html ở đâu trong web là nó tự động chạy
| p#demo { | |
| text-align: center; | |
| font-size: 20px; | |
| margin-top: 0px; | |
| border-style: none; | |
| background: #f7cb16; | |
| border-radius: 10px; | |
| padding: 20px 20px 20px 20px; | |
| display: block !important; | |
| font-weight: normal; | |
| box-shadow: rgb(0 0 0 / 40%) 0 5px 10px; | |
| } | |
|
|
|
| function startCountdown() { | |
| // Set the date we’re counting down to | |
| var countDownDate = new Date(); | |
| countDownDate.setDate(countDownDate.getDate() + 1); // Set the countdown to 24 hours later from now | |
| countDownDate.setHours(14, 41, 25); // Set the time to 18:39:25 | |
| // Update the count down every 1 second | |
| var x = setInterval(function() { | |
| // Get today’s date and time | |
| var now = new Date().getTime(); | |
| // Find the distance between now and the count down date | |
| var distance = countDownDate – now; | |
| // Time calculations for days, hours, minutes and seconds | |
| var days = Math.floor(distance / (1000 * 60 * 60 * 24)); | |
| var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); | |
| var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); | |
| var seconds = Math.floor((distance % (1000 * 60)) / 1000); | |
| // Output the result in an element with id=”demo” | |
| document.getElementById(“demo”).innerHTML = hours + “h “ | |
| + minutes + “m ” + seconds + “s “; | |
| // If the count down is over, start a new countdown after 24 hours | |
| if (distance < 0) { | |
| clearInterval(x); | |
| document.getElementById(“demo”).innerHTML = “EXPIRED”; | |
| // Start a new countdown after 24 hours | |
| startCountdown(); | |
| } | |
| }, 1000); | |
| } | |
| // Start the initial countdown | |
| startCountdown(); | |
chỉnh code countdown

Demo cho code chạy dưới đây luôn nè
Chúc các bạn thành công, code lỗi hoặc như nào các bạn cứ báo cho mình nhé !!!
