Flash Games
 FAQ   Search   Memberlist   Usergroups   Register  Profile   Log in to check your private messages   Log in 


Help needed with this timer code please!!



 

Post new topic   Reply to topic  
   DEVPPL Forum Index -> JavaScript Forum
View previous topic :: View next topic  
Author Message
paulf



Joined: 19 May 2007
Posts: 12

PostPosted: Mon Jun 09, 2008 9:39 am    Post subject: Help needed with this timer code please!! Reply with quote

Hi all I have this timer code which works fine except I am trying to get it to count down from 57min exactly.

You will see from the code that it is basically ... get the time NOW and add 57 mins to it then count down to zero. At zero do something else!

The display needs to be You have X min X secs left

I have got this far but everytime I try to change the code to achieve the above I break it!

Can anyone please help?




Code:
//start countdown



function countdown_clock(year, month, day, hour, minute, format)
         {
         //I chose a div as the container for the timer, but
         //it can be an input tag inside a form, or anything
         //who's displayed content can be changed through
         //client-side scripting.        html_code = '<div id="countdown"></div>';
         
         document.write(html_code);
         
         countdown(year, month, day, hour, minute, format);               
         }
         
function countdown(year, month, day, hour, minute, format)
         {
         Today = new Date();
         Todays_Year = Today.getFullYear() - 2000;
         Todays_Month = Today.getMonth();                 
         
         //Convert both today's date and the target date into miliseconds.                           
         Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(),
                                 Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();                                 
         Target_Date = (new Date(year, month - 1, day, hour, minute, 00)).getTime();                 
         
         //Find their difference, and convert that into seconds.                 
         Time_Left = Math.round((Target_Date - Todays_Date) / 1000);
         
// add 57 min to above time code
      
         if(Time_Left < 0)
         {
            Time_Left = 0;
         //alert('BOOM!');
         }

         
         
         switch(format)
               {
               case 0:
                   //The simplest way to display the time left
                    document.all.countdown.innerHTML = Time_Left + ' seconds';
                    break;
               case 1:
                   //More datailed
                    days = Math.floor(Time_Left / (60 * 60 * 24));
                    Time_Left %= (60 * 60 * 24);
                    hours = Math.floor(Time_Left / (60 * 60));
                    Time_Left %= (60 * 60);
                    minutes = Math.floor(Time_Left / 60);
                    Time_Left %= 60;
                    seconds = Time_Left;
                   
                    dps = 's'; hps = 's'; mps = 's'; sps = 's';
                   //ps is short for plural suffix
                    if(days == 1) dps ='';
                    if(hours == 1) hps ='';
                    if(minutes == 1) mps ='';
                    if(seconds == 1) sps ='';
                   
                    document.all.countdown.innerHTML = days + ' day' + dps + ' ';
                    document.all.countdown.innerHTML += hours + ' hour' + hps + ' ';
                    document.all.countdown.innerHTML += minutes + ' minute' + mps + ' and ';
                    document.all.countdown.innerHTML += seconds + ' second' + sps;
                    break;
               default:
                    document.all.countdown.innerHTML = Time_Left + ' seconds';
               }
               
      //Recursive call, keeps the clock ticking         setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + format + ');', 1000);
         }
[/code]
Back to top
View user's profile Send private message
flabbyrabbit
500+ Club


Joined: 25 Jan 2007
Posts: 569
Location: UK

PostPosted: Tue Jun 10, 2008 12:37 pm    Post subject: Re: Help needed with this timer code please!! Reply with quote

I found a simple countdown script using google, and changed it to fit what you need. Hope this helps:
Code:
<div id="TimeLeft">You have 57 min 0 secs left</div>

<script>
<!--
//
 var sec=0
 var min=57 

function display(){
 if (sec<=0){
    sec=60
    min-=1
 }
    if (min<=-1){
       sec=0
       min+=1
       alert('DONE');
    } else {
       sec-=1
       document.getElementById('TimeLeft').innerHTML="You have "+min+" min "+sec+" secs left"
       setTimeout("display()",1000)
    }
}
display()
-->
</script>



Flabby Rabbit
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
paulf



Joined: 19 May 2007
Posts: 12

PostPosted: Tue Jun 10, 2008 8:05 pm    Post subject: Re: Help needed with this timer code please!! Reply with quote

Fantastic flabby rabbit ... The code does exactly what I wanted it to do. Now, I put it into my code body adjusting tags as appropriate and it works pefectly except for one thing! Because it is timing a quiz as each question is loaded the code initiates again and resets the clock ... starting the countdown again.

So, if I were to put it in an external file and call it timer.js for example, could I call it to initiate just once? If so how do I do it?

Thanks

Paul
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    DEVPPL Forum Index -> JavaScript Forum All times are GMT + 1 Hour
Page 1 of 1

 
 
Welcome to DEVPPL.com
You are not logged in, which means that you can't post in the forums.
Click here to Register

If you are a current member here on DEVPPL, please login below:

User: Pass:
Log me on automatically each visit:

 


Powered by phpBB © 2001, 2005 phpBB Group - Modified by DEVPPL

Flash Games - Sitemap