简体   繁体   English

倒数计时器重置

[英]Countdown Timer Reset

I have made a few countdown timers before but need to make a slight twist to this one. 我之前做了一些倒数计时器,但需要对此稍作改动。 I need to make it countdown to Wednesday every week. 我需要每周将其倒计时至星期三。 So when it does reach Wednesday, it then renews and looks at going to next Wednesday, 7 days in the future. 因此,当到达星期三时,它会续订并考虑到未来7天的下一个星期三。

So far I have used a very simple code block that takes up some space I'm afraid: 到目前为止,我已经使用了一个非常简单的代码块,它担心会占用一些空间:

    var td = new Date("Sep 16, 2015").getTime();
var days, hrs, mins, secs;

function recalc() {
    var curdate = new Date().getTime();
    var secsleft = (td - curdate) / 1000;

    days = parseInt(secsleft / 86400);
    secsleft = secsleft % 86400;

    hrs = parseInt(secsleft / 3600);
    secsleft = secsleft % 3600;

    mins = parseInt(secsleft / 60);
    secs = parseInt(secsleft % 60);
}

setInterval(function() {
    recalc();
    while(days<1 && hrs<1 && mins<1 && secs<1) {td.setDate(td.getDate() + parseInt(7)); recalc();}
    $("#countdown").html(days + " Days " + hrs + " Hours " + mins + " Mins " + secs + " Secs");
}, 1000);

My problem is the last line of code. 我的问题是代码的最后一行。 I am checking to see if it has all reached 0 or below. 我正在检查是否全部达到0或以下。 In my current case, several figures are in the negatives and the line does not trigger. 在我目前的情况下,负数有几个数字,该行不会触发。 Despite this, I am also receiving an error stating that td.getDate() is not a function. 尽管如此,我还收到一条错误消息,指出td.getDate()不是函数。

EDIT: I am fairly certain my only remaining issue is why td.setDate(td.getDate() + parseInt(7)); 编辑:我可以肯定我唯一剩下的问题就是为什么td.setDate(td.getDate() + parseInt(7)); is returning an error of: td.getDate() is not a function. 返回错误: td.getDate()不是函数。

Everything is triggering correctly, all that is required is to add 7 days onto the td (target_date) and then it should rerun the recalc() function. 一切都正确触发,所需要做的就是在td (target_date)上添加7天,然后应该重新运行recalc()函数。

This won't work: 这行不通:

if(days<1 && hrs<1 && mins<1 && secs<1) {td.setDate(td.getDate() + 7);}

Because logically the cannot ALL be less than one at the same time. 因为从逻辑上讲,所有不能同时小于1。 Once they all hit zero, the day switches to the next day. 一旦它们全部达到零,则该天切换到第二天。 The last second of the day is actually 23:59:59. 一天的最后一秒实际上是23:59:59。

You should go with: 您应该选择:

if(days=0 && hrs=0 && mins=0 && secs<2) {td.setDate(td.getDate() + 7);}

I think that should help with part of it. 我认为这应该会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM