TinyPortal

Development => Block Codes => Topic started by: Final60 on February 14, 2009, 03:23:38 AM

Title: [Block] Recurring count down timer UTC
Post by: Final60 on February 14, 2009, 03:23:38 AM
The following is a recurring count down timer. It will count down to a specific day and/or hour, minute, second and repeat the countdown continuously. 


Please add the following into a Script block


<div id="counter">Loading...</div>
<script type="text/javascript">
     function myCounter(o, timeArray){
         var counter = document.getElementById(o);
         if(!counter) {
             return;
         }

         // 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';
             if(showZero){
                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];
            if(day==-1){
                day=dayNow;
            }
             secArray.push({
                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 updateCounter(){
             // 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;
                 if(diff>0){
                     nextIndex=i;
                     break;
                 }
             }
             //
             var diff=secArray[nextIndex].sec-secNow;
             var prevDiff=diff;
             if(diff<0){
                var dayDiff = 6-secArray[nextIndex].day;
                if(secArray[nextIndex].day == -1){
                    dayDiff=0;
                }
                diff=(dayDiff+1)*24*60*60-Math.abs(diff);
             }
             var str='';
             // get message if there is any set
             if(secArray[nextIndex].msg){
                 str=secArray[nextIndex].msg;
             }
             var timeString = toTimeString(diff, secArray[nextIndex].showZero);
             if(str.match('@{counter}')!=null){
                 str=str.replace(/@{counter}/, timeString);
             }else if(str.indexOf(' ')==0){ // message starts with space
                 str=timeString+str;
             }else{ // no specific hint where to put counter, so display it after message
                 str+=timeString;
             }
             counter.innerHTML=str;
        }
     
         setInterval(updateCounter, 1000);
         
     };
myCounter('counter', [ [2, 20, 0, 0, '<b>Event is in<b> @{counter}', false] ]);
</script>




To set the date and time find


myCounter('counter', [ [2, 20, 0, 0, '<b>Event is in<b> @{counter}', false] ]);



The format is Day,hour,minute,second in 24 hours time.
Sunday =0
Monday=1
Tuesday=2
Wednesday=3
Thursday=4
Friday=5
Saturday=6
Daily countdown = -1

To have multiple count down timers in the same block add another


<div id="counter">Loading...</div>

at the top and rename it to "counter2" etc and add another



myCounter('counter', [ [2, 20, 0, 0, '<b>event is in</b> @{counter}', false] ]);


at the bottom and rename it to "counter2" etc.

Example of way to add text with timer


myCounter('counter', [
  [16,0,0, 'There is @{counter} left for event one'],
  [16,0,10, 'Second event starts in '],
  [16,0,20, ' for third event', false]
]);



Enjoy
Title: Re: [Block] Repeating 24 hour count down timer
Post by: Dianna on February 14, 2009, 05:04:29 AM
Hey, this is really cool!  Where do I put in the time that I want it to countdown too?  I want it to countdown to 10:00pm Pacific Standard Time.

ETA:  Oh duh, on the 16,0,0. Is that right? 

So mine would be 10,0,0? 

I'm thinking out loud that I would have to use the 2nd and 3rd counters for other time zones.

Very cool!  Thanks!

Title: Re: [Block] Repeating 24 hour count down timer
Post by: ZarPrime on February 15, 2009, 04:08:16 AM
Final60,

I know it might be a different script, but do you know off the top of your head what I would change to have a repeating countdown that goes off once per week instead of once per day.

For instance, let's say I wanted the timer to countdown to Friday morning at 6 AM every week instead of just 6AM every day.

ZarPrime
Title: Re: [Block] Repeating 24 hour count down timer
Post by: Final60 on February 15, 2009, 04:52:36 PM
Quote from: Dianna on February 14, 2009, 05:04:29 AM
Hey, this is really cool!  Where do I put in the time that I want it to countdown too?  I want it to countdown to 10:00pm Pacific Standard Time.

ETA:  Oh duh, on the 16,0,0. Is that right? 

So mine would be 10,0,0? 

I'm thinking out loud that I would have to use the 2nd and 3rd counters for other time zones.

Very cool!  Thanks!

This script is set for UTC which is GMT+0 so for yor example you would use the following;

myCounter('counter', [
  [06,0,0, 'event starts in '],
]);


If I've worked it out right PST is GMT-8.
So for you to get the counter to count down to 10pm (PST) daily you have it set to 06(am gmt+0),0(mins),0(secs). Its possible to use timeoffset, but in both cases you need to know how many hours to offset and change the code accordingly, so this may be the simpler option.
Title: Re: [Block] Recurring 24 hour count down timer UTC
Post by: Final60 on February 15, 2009, 05:12:34 PM
Quote from: ZarPrime on February 15, 2009, 04:08:16 AM
Final60,

I know it might be a different script, but do you know off the top of your head what I would change to have a repeating countdown that goes off once per week instead of once per day.

For instance, let's say I wanted the timer to countdown to Friday morning at 6 AM every week instead of just 6AM every day.

ZarPrime

For a weekly timer this should work, I havent tested it yet though


<script type="text/javascript">
     function myCounter(o, timeArray){
         var counter = document.getElementById(o);
         if(!counter) {
             return;
         }

         // 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';
             if(showZero){
                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];
            if(day==-1){
                day=dayNow;
            }
             secArray.push({
                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 updateCounter(){
             // 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;
                 if(diff>0){
                     nextIndex=i;
                     break;
                 }
             }
             //
             var diff=secArray[nextIndex].sec-secNow;
             var prevDiff=diff;
             if(diff<0){
                var dayDiff = 6-secArray[nextIndex].day;
                if(secArray[nextIndex].day == -1){
                    dayDiff=0;
                }
                diff=(dayDiff+1)*24*60*60-Math.abs(diff);
             }
             var str='';
             // get message if there is any set
             if(secArray[nextIndex].msg){
                 str=secArray[nextIndex].msg;
             }
             var timeString = toTimeString(diff, secArray[nextIndex].showZero);
             if(str.match('@{counter}')!=null){
                 str=str.replace(/@{counter}/, timeString);
             }else if(str.indexOf(' ')==0){ // message starts with space
                 str=timeString+str;
             }else{ // no specific hint where to put counter, so display it after message
                 str+=timeString;
             }
             counter.innerHTML=str;
        }
     
         setInterval(updateCounter, 1000);
         
     };
myCounter('counter', [ [DayOfWeek, hour, min, sec, 'Counter @{counter}', false] ]);
</script>


Title: Re: [Block] Recurring 24 hour count down timer UTC
Post by: ZarPrime on February 15, 2009, 08:03:41 PM
Hi Final60,

First of all, thanks for the code.  However, there is something wrong with the weekly code, but I'm not sure what it is.  Here's what's happening ...

1.  When I have a block with the Weekly Countdown in it, and the parameters set to ...
myCounter('counter', [ [5, 6, 0, 0, 'Next Turns are Due in @{counter}', false] ]);
... The weekly block doesn't work.  See "Turn Countdown Test" Block in pic 1.

2.  When I have a block with the 24 Hour Countdown in it, and the parameters set to ...
myCounter('counter', [
  [6,0,0, 'There is @{counter} left for event one'],
  [6,0,10, 'Second event starts in '],
  [6,0,20, ' for third event', false]

... the block works perfectly for the 24 countdown.  See "Countdown Test" Block in pic 2.

*** Now here's the strange part ***

3.  When I have both blocks turned on, the "Turn Countdown Test" Block still doesn't work but now the "Countdown Test" Block works the way I want the "Turn Countdown Test" Block to work.  In other words, the 24 hour block picks up and displays the countdown the way the weekly block should be.  See both Blocks in pic 3.

For some reason, I can't seem to put my brain around this and figure out what is happenng here.  I just want the weekly countdown, not the 24 hour one.

Any Clues?

ZarPrime
Title: Re: [Block] Recurring 24 hour count down timer UTC
Post by: Final60 on February 15, 2009, 09:13:22 PM
this seem to work.

DayOfWeek would be -1 for 24h interval counter and for weekly counters 0=Sunday, 1=Monday,... 6=Saturday
myCounter('counter', [ [DayOfWeek, hour, min, sec, 'Counter @{counter}', false] ]);

make sure if you have both counters running at the same time that they don't have the same ID, counter, counter1, counter2, counter3 etc.

For example this full code


<div id="counter3">Loading...</div>
<script type="text/javascript">
     function myCounter(o, timeArray){
         var counter = document.getElementById(o);
         if(!counter) {
             return;
         }

         // 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';
             if(showZero){
                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];
            if(day==-1){
                day=dayNow;
            }
             secArray.push({
                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 updateCounter(){
             // 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;
                 if(diff>0){
                     nextIndex=i;
                     break;
                 }
             }
             //
             var diff=secArray[nextIndex].sec-secNow;
             var prevDiff=diff;
             if(diff<0){
                var dayDiff = 6-secArray[nextIndex].day;
                if(secArray[nextIndex].day == -1){
                    dayDiff=0;
                }
                diff=(dayDiff+1)*24*60*60-Math.abs(diff);
             }
             var str='';
             // get message if there is any set
             if(secArray[nextIndex].msg){
                 str=secArray[nextIndex].msg;
             }
             var timeString = toTimeString(diff, secArray[nextIndex].showZero);
             if(str.match('@{counter}')!=null){
                 str=str.replace(/@{counter}/, timeString);
             }else if(str.indexOf(' ')==0){ // message starts with space
                 str=timeString+str;
             }else{ // no specific hint where to put counter, so display it after message
                 str+=timeString;
             }
             counter.innerHTML=str;
        }
     
         setInterval(updateCounter, 1000);
         
     };
myCounter('counter3', [ [6, 0, 0, 0, 'Counter @{counter}', false] ]);
</script>

^^
Title: Re: [Block] Recurring 24 hour count down timer UTC
Post by: ZarPrime on February 15, 2009, 09:31:55 PM
Fantastic.  That works great. :up:

Thanks,
ZarPrime
Title: Re: [Block] Recurring count down timer UTC
Post by: ZarPrime on February 19, 2009, 05:11:27 PM
Final60,

It seems there is some sort of quirk in the weekly code.  When it ticks a day off, it seems to be adding another day or something.  I am going to watch this thing for a whole week and see what it is doing.  It's kind of hard to track down the quirk because it only ticks off a day just once a day.

Right now, I have it set to countdown to zero at 6AM UTC on Friday morning, which is midnight in the eastern time zone.  I just wanted to give you a heads up that there is something quirky about the code.  Here's the code I'm currently using and it should countdown to zero Thursday night at midnight eastern time.  We'll see what happens ...

<div id="counter3">Loading...</div>
<script type="text/javascript">
     function myCounter(o, timeArray){
         var counter = document.getElementById(o);
         if(!counter) {
             return;
         }

         // 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';
             if(showZero){
                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];
            if(day==-1){
                day=dayNow;
            }
             secArray.push({
                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 updateCounter(){
             // 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;
                 if(diff>0){
                     nextIndex=i;
                     break;
                 }
             }
             //
             var diff=secArray[nextIndex].sec-secNow;
             var prevDiff=diff;
             if(diff<0){
                var dayDiff = 6-secArray[nextIndex].day;
                if(secArray[nextIndex].day == -1){
                    dayDiff=0;
                }
                diff=(dayDiff+1)*24*60*60-Math.abs(diff);
             }
             var str='';
             // get message if there is any set
             if(secArray[nextIndex].msg){
                 str=secArray[nextIndex].msg;
             }
             var timeString = toTimeString(diff, secArray[nextIndex].showZero);
             if(str.match('@{counter}')!=null){
                 str=str.replace(/@{counter}/, timeString);
             }else if(str.indexOf(' ')==0){ // message starts with space
                 str=timeString+str;
             }else{ // no specific hint where to put counter, so display it after message
                 str+=timeString;
             }
             counter.innerHTML=str;
        }
     
         setInterval(updateCounter, 1000);
         
     };
myCounter('counter3', [ [5, 5, 0, 0, '<center><b>Next Turns are Due in </b><p class="smalltext"> @{counter}</center>', false] ]);
</script>


If anything pops into your head as to why this is happening, please let me know.

ZarPrime
Title: Re: [Block] Recurring count down timer UTC
Post by: boomslanger on February 20, 2009, 07:50:17 PM
Thanks for the great code, it has come in handy counting down to a special event for next year.

I attempted to add code to countdown in weeks but had no luck. Is it possible to have the countdown show weeks to an event?

Thanks
Title: Re: [Block] Recurring count down timer UTC
Post by: Xarcell on February 20, 2009, 11:42:43 PM
Awesome script, thanks.
Title: Re: [Block] Recurring count down timer UTC
Post by: Final60 on February 21, 2009, 12:19:35 AM
QuoteIt seems there is some sort of quirk in the weekly code.  When it ticks a day off, it seems to be adding another day or something.  I am going to watch this thing for a whole week and see what it is doing.

Strange. As far as I am aware the script should work fine. Make sure your script isn't conflicting against another. like somwhere else there is "counter3" also.

QuoteIs it possible to have the countdown show weeks to an event?
I'll work on this soon for you :)
Title: Re: [Block] Recurring count down timer UTC
Post by: ZarPrime on February 21, 2009, 12:49:32 AM
Final60,

Saw you on. 

Last night, at midnight, when it was supposed to countdown to zero it added 2 days (weird).  Sometime during the day today it took off a day, and right now, it says 4 hours and 15 minutes are left.  It was set to go to zero at Midnight last night (Friday Morning), so right now it should say 6 days, 4 hours and 15 minutes but it doesn't.

I don't think I have anything else that should be interfering with this, but I'm going to look over the weekend.

ZarPrime
Title: Re: [Block] Recurring count down timer UTC
Post by: Dianna on February 25, 2009, 01:04:23 AM
Quote from: Final60 on February 15, 2009, 04:52:36 PM
Quote from: Dianna on February 14, 2009, 05:04:29 AM
Hey, this is really cool!  Where do I put in the time that I want it to countdown too?  I want it to countdown to 10:00pm Pacific Standard Time.

ETA:  Oh duh, on the 16,0,0. Is that right? 

So mine would be 10,0,0? 

I'm thinking out loud that I would have to use the 2nd and 3rd counters for other time zones.

Very cool!  Thanks!

This script is set for UTC which is GMT+0 so for yor example you would use the following;

myCounter('counter', [
  [06,0,0, 'event starts in '],
]);


If I've worked it out right PST is GMT-8.
So for you to get the counter to count down to 10pm (PST) daily you have it set to 06(am gmt+0),0(mins),0(secs). Its possible to use timeoffset, but in both cases you need to know how many hours to offset and change the code accordingly, so this may be the simpler option.


Hi Final60, thank you so much! I love the counter!!!!  For my clients this is an awesome feature for their site!  I just discovered one thing for my client that is a snag in me being able to use it. My client has a weekly talk radio show every night 6 nights a week. The countdown ROCKS on the site to show the countdown to the next show. I just realize how they do not do a show on Sunday. So is there a way to tweak the coding to count down 48 hours on the 7th day so it would skip the Sunday night? 

here's my site page just in case www.schreeandbaby.com/index.php and it's on the right hand block.
Title: Re: [Block] Recurring count down timer UTC
Post by: ZarPrime on March 07, 2009, 12:04:31 PM
Final60,

I have been trying to sort out the weekly counter errors I've been having again.  I've been watching this thing for the past couple of weeks and it's still just not working right.  I know the weekly counter was not the original purpose of this snippet, but I would like to figure out why these quirks are happening.  I have it set to tick down to zero on day 5, and 5AM UTC (Friday Morning at 5AM).  That means that in my time zone, it should count down to zero at Midnight Thursday night (US Eastern time zone).

Every once in a while, a day will be added, and then taken back off later in the day.  Right now, it's Saturday morning at 7 AM Eastern time here.  The timer should say "5 days 17 hrs 0 min 0 sec" but it says "17 hrs 0 min 0 sec" instead.

I used to be pretty good at spotting errors in script and have looked at the code inside and out, and I also had another coder take a look at it, and he thinks it looks OK as well, but I can't not figure out what is wrong with it.  At your suggestion, I changed all instances of "counter" in my code to "countre" just to make sure that nothing else was interfering with it.  I've also set this code up to run a regular html page that I have running in another tab in my browser, and it is doing the exact same thing as it's doing in my Forum, so it's not anything to do with my Forum setup.

I am going to ask you to take a second look at this code one more time, and if we can't figure out what is causing the problems, I am just not going to be able to use it, as much as I would like to.  The current code I'm using is shown below.

Thanks,
ZarPrime

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

         // 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';
             if(showZero){
                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];
            if(day==-1){
                day=dayNow;
            }
             secArray.push({
                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;
                 if(diff>0){
                     nextIndex=i;
                     break;
                 }
             }
             //
             var diff=secArray[nextIndex].sec-secNow;
             var prevDiff=diff;
             if(diff<0){
                var dayDiff = 6-secArray[nextIndex].day;
                if(secArray[nextIndex].day == -1){
                    dayDiff=0;
                }
                diff=(dayDiff+1)*24*60*60-Math.abs(diff);
             }
             var str='';
             // get message if there is any set
             if(secArray[nextIndex].msg){
                 str=secArray[nextIndex].msg;
             }
             var timeString = toTimeString(diff, secArray[nextIndex].showZero);
             if(str.match('@{countre}')!=null){
                 str=str.replace(/@{countre}/, timeString);
             }else if(str.indexOf(' ')==0){ // message starts with space
                 str=timeString+str;
             }else{ // no specific hint where to put countre, so display it after message
                 str+=timeString;
             }
             countre.innerHTML=str;
        }
     
         setInterval(updatecountre, 1000);
         
     };
mycountre('countre3', [ [5, 5, 0, 0, '<center><b>Next Turns are Due in </b><p class="smalltext"> @{countre}</center>', false] ]);
</script>
Title: Re: [Block] Recurring count down timer UTC
Post by: chesterfield on April 08, 2009, 11:17:39 AM
Just noticed as the clocks have changed the timer is now an hour in front? is there a way to detect this seasonal change so that the timer works all year round?

thanks
mark
Title: Re: [Block] Recurring count down timer UTC
Post by: Min2001 on April 26, 2009, 11:08:34 AM
There is nothing wrong with the script you'r using but you are overlooking something very important if you wanted to set the time to be 5PM it needs to be inputted as 17 as it is a 24 hour clock. also take into account when adding the times you dont forget to put the clocks back or forward 1 hour according to daylight saving time....great script by the way :)
Title: Re: [Block] Recurring count down timer UTC
Post by: sandmannd on April 28, 2009, 01:06:09 AM
Not quite sure what I'm doing wrong with this. I can choose "html/javascript" I assume that's what I choose. I put the code in and go to save it and it just spins saying "loading". If I go to my home page to see it, it does the same thing. Any advice on it?
Title: Re: [Block] Recurring count down timer UTC
Post by: sandmannd on April 28, 2009, 01:40:25 AM
I got it fixed, put it in as PHP code and then saved it as Javascript and it works. Very nice mod.  :up:
Title: Re: [Block] Recurring count down timer UTC
Post by: solio on May 20, 2009, 01:56:18 AM
 ;D

Hey nice script, thank u .... however, I have 1 problem....

I'm using it to repeat each day, and it works very nicely....

4 hrs 23mins 14secs

(the day does not display which is correct of course, since it is only being used for 24hrs).

HOWEVER I notice when it gets down to the last 10 seconds, it then displays:

1 day 0 hrs 0 mins 10sec

the "1 day" part comes from ???  How can I remove that?

Also, it seems that at the very same time (10 second mark), IE crashes - it must be a bug in the script at that moment methinks!

Any help appreciated - otherwise great script!

(here's my code)



<div id="counter">
  <div align="left">Loading...</div>
</div>
<div align="left">
  <script type="text/javascript">
     function myCounter(o, timeArray){
         var counter = document.getElementById(o);
         if(!counter) {
             return;
         }

         // 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';
             if(showZero){
                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];
            if(day==-1){
                day=dayNow;
            }
             secArray.push({
                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 updateCounter(){
             // 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;
                 if(diff>0){
                     nextIndex=i;
                     break;
                 }
             }
             //
             var diff=secArray[nextIndex].sec-secNow;
             var prevDiff=diff;
             if(diff<0){
                var dayDiff = 6-secArray[nextIndex].day;
                if(secArray[nextIndex].day == -1){
                    dayDiff=0;
                }
                diff=(dayDiff+1)*24*60*60-Math.abs(diff);
             }
             var str='';
             // get message if there is any set
             if(secArray[nextIndex].msg){
                 str=secArray[nextIndex].msg;
             }
             var timeString = toTimeString(diff, secArray[nextIndex].showZero);
             if(str.match('@{counter}')!=null){
                 str=str.replace(/@{counter}/, timeString);
             }else if(str.indexOf(' ')==0){ // message starts with space
                 str=timeString+str;
             }else{ // no specific hint where to put counter, so display it after message
                 str+=timeString;
             }
             counter.innerHTML=str;
        }
     
         setInterval(updateCounter, 1000);
         
     };
myCounter('counter', [ [-1, 24, 00, 10, '<span class="lcdtext">Today\'s deadline </span><span class="lcdstyle">@{counter} </span>', false] ]);
</script>
 

Title: Re: [Block] Recurring count down timer UTC
Post by: sandmannd on April 15, 2010, 02:24:48 AM
I tried searching and can't find anything else on this. Does anyone remember how to make it show weeks?
Title: Re: [Block] Recurring count down timer UTC
Post by: ESFclandoteu on May 28, 2010, 09:37:04 PM
Hi, thanks for this script, it's just what I'm looking for; HOWEVER....!

Is there anything I can add so that upon the countdown completing it will display a message for say, 5 hours and then start the countdown again...

eg...
Countdown set for 1800GMT daily
1800GMT arrives countdown reaches 0
Message displayed "The server is now online"
set time passes..
Countdown restarts showing "18hrs until server is online"

Any help would be great, I've searched high and low but cant find anything that does this