How to make a countdown timer by taking data from data?
-
I want to make each div element a countdown timer, the time data will be contained in data.
Please tell me why it doesn't work.
The code was taken from here https://html5css.com/howto/howto_js_countdown.php
<div data-test="Oct 10, 2020 20:37:25" class="text"> </div> <div data-test="Oct 10, 2020 19:37:25" class="text"> </div> <div data-test="Oct 10, 2020 18:37:25" class="text"> </div> <div data-test="Oct 11, 2020 18:37:25" class="text"> </div> <div data-test="Oct 13, 2020 18:37:25" class="text"> </div> <div data-test="Oct 13, 2021 18:37:25" class="text"> </div>
$('.text').text(function() { var countDownDate = new Date(this.dataset.test).getTime(); timeGet($(this), countDownDate); }); function timeGet(date, ints){ let now = new Date().getTime(); // Find the distance between now an the count down date let distance = date - now; // Time calculations for days, hours, minutes and seconds let days = Math.floor(distance / (1000 * 60 * 60 * 24)); let hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); let seconds = Math.floor((distance % (1000 * 60)) / 1000); // Output the result in an element with id="demo" ints.text(hours + ":" + minutes + ":" + seconds); // If the count down is over, write some text if (distance < 0) { clearInterval(x); document.getElementById("demo").innerHTML = "EXPIRED"; } else { setTimeout(function(){ timeGet(date, ints); }, 1000); } }
JavaScript Elizabeth Cook, Oct 23, 2019 -
In my opinion you confused the arguments:
timeGet ($ (this), countDownDate);
function timeGet (date, ints) {
Anonymous -
Adam Dickerson
2 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!