1

I was browsing around at different ideas for a countdown script. I like how this one works because you can enter an exact date instead of having to deal with subtracting 1 for numeric dates because they start on 0.

function cdtd() {
    var xmas = new Date(" 00:01:00");
    var now = new Date();
    var timeDiff = xmas.getTime() - now.getTime();
    if (timeDiff <= 0) {
                clearTimeout(timer);
        document.write("Christmas is here!");
        // Run any code needed for countdown completion here
        }
    var seconds = Math.floor(timeDiff / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    var days = Math.floor(hours / 24);
    hours %= 24;
        minutes %= 60;
        seconds %= 60;
    document.getElementById("daysBox").innerHTML = days;
    document.getElementById("hoursBox").innerHTML = hours;
    document.getElementById("minsBox").innerHTML = minutes;
    document.getElementById("secsBox").innerHTML = seconds;
    var timer = setTimeout('cdtd()',1000);
}

HTML

Days Remaining:
<div id="daysBox"></div>
Hours Remaining:
<div id="hoursBox"></div>
Minutes Remaining:
<div id="minsBox"></div>
Seconds Remaining:
<div id="secsBox"></div>

Runs Function

<script type="text/javascript">cdtd();</script>

I need it to be in a function like this one; however, I've got a project now that requires the countdown to repeat weekly to detect a specific day of week and time.

So, for instance:

Sunday 10:00am

It needs to countdown until it reaches to that point. And after Sunday at 10:00am, it needs to start over and countdown til the next Sunday at 10:00am.

How can I make this happen. Again, I like the overall structure of how the example countdown works; however, I'm open to suggestions on what others think if you have a better scripted countdown timer.

Thanks!

KDJ
  • 309
  • 2
  • 14
  • You should probably do this server side. Client side countdowns will be dependent on users time. So potentially every user will see a different time/count-down. – Mark Apr 15 '15 at 17:52
  • Can you add a code example of how to do this server side? I'm honestly not even sure how to. I did read about doing it server side though and I would like to. – KDJ Apr 15 '15 at 17:54
  • What lanuage are you using? – Mark Apr 15 '15 at 17:54
  • I can use both PHP and Javascript. – KDJ Apr 15 '15 at 17:55
  • You have the countdown code. Now you just need to write a code that will check when will be the next week day that you input. Like, if today is WED 15-Apr, and your input is SUN, you will easily calculate the date will be 19-Apr. Then put that date in countdown code. Try yourself before asking help. – Han Apr 15 '15 at 17:59
  • I'd like for the code to work automatically to where I don't have to come in and update the code every week. That is my goal in asking this question. – KDJ Apr 15 '15 at 18:21

1 Answers1

1

Something like this would work:

However, I would try and replace the var now with server time (via PHP), so you can be sure all user are seeing the same count down.

 function plural(s, i) {
      return i + ' ' + (i > 1 ? s + 's' : s);
    }

function sundayDelta(offset) {
  // offset is in hours, so convert to miliseconds
  offset = offset ? offset * 60 * 60 * 1000 : 0;
  var now = new Date(new Date().getTime() + offset);
  var days = 7 - now.getDay() || 7;
  var hours = 24 - now.getHours();
  var minutes = 60 - now.getMinutes();
  var seconds = 60 - now.getSeconds();
  return [plural('day', days),
          plural('hour', hours),
          plural('minute', minutes), 
          plural('second', seconds)].join(' ');
}

// Save reference to the DIV
$refresh = $('#refresh');

$refresh.text('This page will refresh in ' + sundayDelta());

// Update DIV contents every second
setInterval(function() {
  $refresh.text('This page will refresh in ' + sundayDelta());
}, 1000);
Mark
  • 4,773
  • 8
  • 53
  • 91
  • If you have the time, can you show me how to set server time with PHP and Javascript? – KDJ Apr 15 '15 at 18:16
  • Not really a php guy, but this looks like it should cover it.... http://stackoverflow.com/questions/470617/get-current-date-and-time-in-php – Mark Apr 15 '15 at 18:18
  • How do I display the countdown on the page? I added your code; however, not sure exactly how to call it. But thanks for your input on php server time. – KDJ Apr 15 '15 at 18:22
  • awesome! Forgot to add `$( document ).ready(function() {` around the javascript. Works now. – KDJ Apr 15 '15 at 18:28
  • does this code account for the specific time on Sunday. Sunday 10:00am? Sorry, working on how to read the code you wrote. Just saw that it worked. Fairly new to javascript. – KDJ Apr 15 '15 at 18:30
  • is there a way to do it where I can call it in function from another point in page. Example: SundayDelta(10:00) or whatever will call that function and place 10:00 where it needs to go to countdown til 10:00am. Just using 10:00 as an example, this can be formatted however it needs to be. I was hoping to be able to call this using a function and be able to adjust the time I am calling based on the info I send to function. That way I can use multiple of them. Also, which part do I adjust to change days to a different day? two questions in one. I'm hoping after this I can stop bothering you. – KDJ Apr 15 '15 at 18:49
  • Here is an updated fiddle. Time is a pain in the ass to deal with. I cant spend any more time on this..... I suggest trying a little more, and posting another post once you get closer. Also, your last comment is possible. https://jsfiddle.net/cwqxzm6o/7/ – Mark Apr 15 '15 at 18:59