[英]setInterval(); not working. I am trying to get my countdown to go down but it gets stuck and I can't seem to find whats wrong with it
(HTML)
<div class="countdownholder">
<div class="content">
<div class="contentHeader">
<h3>Days until my 22nd birthday</h3>
</div>
<div class="countdownHolder">
<div class="time1" id="timeDay"><p></p></div>
<div class="time1" id="timeHours"></div>
<div class="time1" id="timeMinutes"></div>
<div class="time1" id="timeSeconds"></div>
</div>
</div>
</div>
<script src="js/main.js"></script>
(javascript)
var birthdayDate = new Date('Jan 04, 2024 00:01:00');
var currentTime = new Date();
var timeLeft = birthdayDate - currentTime; (for current day)
var suspectedDays = document.getElementById("timeDay");
var suspectedHours = document.getElementById("timeHours");
var suspectedMins = document.getElementById("timeMinutes");
var suspectedSecs = document.getElementById("timeSeconds");
function getTime(){
//making the computer do math to get current time//
var d = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
var h = Math.floor((timeLeft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var m = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60));
var s = Math.floor((timeLeft % (1000 * 60)) / 1000);
suspectedDays.innerHTML = d;
suspectedHours.innerHTML = h;
suspectedMins.innerHTML = m;
suspectedSecs.innerHTML = s;
}
setTimeout(getTime, 1000); //<-- Does not work
getTime(birthdayDate, currentTime(current time), timeLeft);
I was recommended to use the interval command but it does not go to plan and the countdown freezes.我被推荐使用 interval 命令,但它没有 go 来计划并且倒计时冻结。 I looked at other examples but thus end up on the same problem as before.我查看了其他示例,但最终遇到了与以前相同的问题。 my freidd even looked at this and was stumped.我的 freidd 甚至看着这个,被难住了。
Consider moving var currentTime
and var timeLeft
into the function getTime
so they actually get re-evaluated each time the getTime
function runs.考虑将var currentTime
和var timeLeft
移动到 function getTime
中,这样每次getTime
function 运行时它们实际上都会重新评估。 As written it seems like this will write the same time to the elements every time the function runs.如所写,每次 function 运行时,这似乎都会将相同的时间写入元素。
you need to put currentTime
in the function getTime()
so that getTime
methods can calculate remain time whenever called.您需要将currentTime
放入 function getTime()
中,以便getTime
方法可以在调用时计算剩余时间。
function getTime(){
var currentTime = new Date();
var timeLeft = birthdayDate - currentTime;
var d = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
var h = Math.floor((timeLeft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var m = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60));
var s = Math.floor((timeLeft % (1000 * 60)) / 1000);
suspectedDays.innerHTML = d;
suspectedHours.innerHTML = h;
suspectedMins.innerHTML = m;
suspectedSecs.innerHTML = s;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.