0

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.

1 Answer 1

0

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.

2
  • But if indeed as any user will view the countdown timer, it will start looking for today Sep 16th, assuming they look on Sep 30th the countdown will be far passed 0:0:0:0 wouldn't it? Sep 16, 2015 at 18:14
  • In addition, I've tested that theory and am getting a result of 0 Days -19 Hours - 15 Minutes Sep 16, 2015 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.