Help needed with this timer code please!!
|
| View previous topic :: View next topic |
| Author |
Message |
paulf
Joined: 19 May 2007 Posts: 12
|
Posted: Mon Jun 09, 2008 9:39 am Post subject: Help needed with this timer code please!! |
|
|
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 |
|
 |
|
|
flabbyrabbit 500+ Club

Joined: 25 Jan 2007 Posts: 569 Location: UK
|
Posted: Tue Jun 10, 2008 12:37 pm Post subject: Re: Help needed with this timer code please!! |
|
|
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 |
|
 |
paulf
Joined: 19 May 2007 Posts: 12
|
Posted: Tue Jun 10, 2008 8:05 pm Post subject: Re: Help needed with this timer code please!! |
|
|
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 |
|
 |
|
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:
|
|
|
|