It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

Help needed with this timer code please!!

Help needed with this timer code please!!

Postby paulf on Mon Jun 09, 2008 8:39 am

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: Select all
//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]
paulf
 
Posts: 12
Joined: Sat May 19, 2007 9:58 am

Postby flabbyrabbit on Tue Jun 10, 2008 11:37 am

I found a simple countdown script using google, and changed it to fit what you need. Hope this helps:
Code: Select all
<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
Image
User avatar
flabbyrabbit
500+ Club
 
Posts: 706
Joined: Thu Jan 25, 2007 1:10 pm
Location: Midlands, England

Postby paulf on Tue Jun 10, 2008 7:05 pm

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
paulf
 
Posts: 12
Joined: Sat May 19, 2007 9:58 am


Who is online

Users browsing this forum: No registered users and 5 guests