简体   繁体   中英

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.

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. 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.

EDIT: I am fairly certain my only remaining issue is why td.setDate(td.getDate() + parseInt(7)); is returning an error of: td.getDate() is not a function.

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.

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. Once they all hit zero, the day switches to the next day. The last second of the day is actually 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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