简体   繁体   English

设置间隔(); 不工作。 我正在尝试倒计时到 go,但它卡住了,我似乎找不到它有什么问题

[英]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 currentTimevar 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.

相关问题 我无法使用任何JSLink。 我究竟做错了什么 - I am not able to get any of my JSLink working. What am I doing wrong 似乎无法更新我的 GET 请求数据,我做错了什么 - Can't seem to update my GET request data, what am I doing wrong 我的javascript函数似乎无法正常工作。 我不知道为什么 - My javascript function doesn't seem to be working. I can't figure out why 我对数组的推送不起作用。 我究竟做错了什么? - My push to a array is not working. What am i doing wrong? 我的JavaScript无法加载外部CSS文件。 我究竟做错了什么? - my javascript for loading external css files isn't working. what am i doing wrong? 我正在尝试使用 MutationObserver 检测特定元素的更改,但它不起作用。 我究竟做错了什么? - I am trying to detect changes on a particular element using the MutationObserver but it is not working. What am I doing wrong? 我正在尝试使用javascript更改表单元格的属性,并且找到了我的建议,但似乎无法使它们正常工作 - I am trying to change the properties of a table cell with javascript and have found my suggestions but I can't seem to get them to work 我正在尝试运行脚本,但是我执行的步骤不起作用。 我哪里出问题了? - I am trying to run a script but the steps I have taken are not working. Where have I gone wrong? 我已经盯着我的代码几个小时了,找不到它出了什么问题。 -Javascript - I've been staring at my code for hours and can't find whats wrong with it. - Javascript 我正在尝试使用传单javascript库可视化地图上的某些数据点。您能告诉我我的代码有什么问题吗? - I am trying to visualise some datapoint on a map using leaflet javascript library.Can you tell me whats wrong with my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM