View Full Version : Recurring count down timer UTC

05-24-2009, 12:18 AM

I'm new here, but I've watched Dynamic Drive for a very long time. I am hoping that someone with more javascript knowledge than I can help me sort out a problem with a script. I didn't write this script but I have checked through it and everything seems to be right but it acts quirky sometimes. The writer and I have exchanged posts on this and he can't seem to figure out where the quirk is coming from either.

This script was orignally submitted as a Block Code Snippet on the TinyPortal Forums and a link to that topic is here ...

What the script is supposed to do is countdown to a particular day and time every week and for the most part it works correctly. However, sometimes, it does not display the number of days to the event correctly. For instance, I have it set to countdown to 5AM GMT on Friday Morning and the time where I am is currently 7:59 PM on Sunday evening EDT but the timer says that there are 5 hours 1 min and 0 sec to go to the event trigger. When it turned 8:00 PM a few minutes ago, the countdown added a day and now says 1 days 4 hrs 57 min 55 sec. Both of these are wrong. It should say 4 days 4 hrs 57 min 55 sec right now but it doesn't say that. It has been frustrating trying to track down why this script is behaving in this manner. I would be grateful if one of you experts could take a look at this and tell me how to fix it.

The format that is supposed to control the configuration is in this line near the bottom of the script ...

mycountre('countre3', [ [5, 5, 0, 0, '<center><b>Next Turns are Due in </b><p class="smalltext"> @{countre}</center>', false] ]);

The first 5 is for Friday, the second 5 is for 5 AM UTC, and the 2 zeros are the minutes and seconds, where Format is Day, hour, minute, second in 24 hours time.
Sunday =0
Daily countdown = -1

The actual code I am using is shown at the bottom of this post below my name.

Thanks in advance for any help or insight anyone can give me that will enable me to solve this.


<div id="countre3">Loading...</div>
<script type="text/javascript">
function mycountre(o, timeArray){
var countre = document.getElementById(o);
if(!countre) {

// helper functions
function mksec(day, h, m, s){ return day*24*60*60+h*60*60+m*60+s; }
function toTimeString(sec, showZero){
var d=Math.floor(sec/(60*60*24))
var h=Math.floor(sec/(60*60)%24);
var m=Math.floor((sec/60) % 60);
var s=sec % 60;
var ret=d+'days '+h+'hrs '+m+'min '+s+'sec';
return ret;
}else if(d==0 && h==0 && m==0){
return s+'sec';
}else if(d==0){
return h+'hrs '+m+'min '+s+'sec';
}else if(d==0 && h==0){
return m+'min '+s+'sec';
}else {
return ret;
var secArray = [];
var dayNow = new Date().getDay();
for(var i=0;i<timeArray.length;i++){
var day=timeArray[i][0];
day: timeArray[i][0],
sec: mksec(day, timeArray[i][1], timeArray[i][2], timeArray[i][3]),
msg: timeArray[i][4] || false,
showZero: timeArray[i][5] || false
secArray.sort(function(a,b){ return a.sec-b.sec;});

// timer code - will be called around each second (~1000 ms)
function updatecountre(){
// get current UTC time in seconds
var d=new Date();
var secNow = mksec(d.getDay(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds());
// find next event
var nextIndex=0;
for(var i=0;i<secArray.length; i++){
var diff = secArray[i].sec-secNow;
var diff=secArray[nextIndex].sec-secNow;
var prevDiff=diff;
var dayDiff = 6-secArray[nextIndex].day;
if(secArray[nextIndex].day == -1){
var str='';
// get message if there is any set
var timeString = toTimeString(diff, secArray[nextIndex].showZero);
str=str.replace(/@{countre}/, timeString);
}else if(str.indexOf(' ')==0){ // message starts with space
}else{ // no specific hint where to put countre, so display it after message

setInterval(updatecountre, 1000);

mycountre('countre3', [ [5, 5, 0, 0, '<center><b>Next Turns are Due in </b><p class="smalltext"> @{countre}</center>', false] ]);

06-15-2009, 08:21 PM
I posted the above message 3 weeks ago now. There have been 318 views of the post now without a response from anyone. I was actually hoping that someone with more javascript experience than I would be interested in helping me solve the problem, but I guess that is not what this Forum is for. :(

Since this seems to be the case, I have begun work on a php solution which should allow a more dynamic solution.

To the Moderators: Feel free to delete this topic since it appaears to have been a waste of your bandwidth and my time to write it.

Thanks to all who thought about helping,