Simple Machines Community Forum

General Community => Scripting Help => Topic started by: MrCue on September 06, 2004, 11:43:48 AM

Title: JavaScript Count down timer.
Post by: MrCue on September 06, 2004, 11:43:48 AM
Can anyone write a countdown timer that takes seconds as an argument, and outputs

weeks, days, hours, minutes and seconds (provided each isnt empty)

and write it to the page every second.

I did attepmt this myself today, but i cant find a JS equivelant for foreach($array as $k => $v) or print_r($array), if you know of one, let me know and i'll try again.
Title: Re: JavaScript Count down timer.
Post by: Owdy on September 06, 2004, 11:57:32 AM
http://www.google.fi/search?hl=fi&ie=UTF-8&q=javascripts+countdown&btnG=Hae&lr=

First result
Title: Re: JavaScript Count down timer.
Post by: MrCue on September 06, 2004, 12:04:02 PM
I looked at the results on google, and they dont do it right. That one write to a box and doesnt include weeks, and they are based on time until x date, not x seconds = w,d,h,m,s
Title: Re: JavaScript Count down timer.
Post by: Owdy on September 06, 2004, 12:07:01 PM
Quote from: MrCue on September 06, 2004, 12:04:02 PM
I looked at the results on google, and they dont do it right. That one write to a box and doesnt include weeks, and they are based on time until x date, not x seconds = w,d,h,m,s

http://www.google.fi/search?hl=fi&ie=UTF-8&q=javascripts+countdown+weeks%2C+days%2C+hours%2C+minutes&btnG=Hae&lr=

First result ;D

<script language="javascript">
/* *********** USAGE *********
### Author.......: Andreas Burger
### email........: [email protected]
### Last Change..: 06.03.2004

#################################
### Free to use for everyone! ###
#################################

To use the Script, you only have to set the date you are looking forward to
in the variable "todate" (second Line after this Comment).

Possible prints are:
Weeks, Days, Hours, Minutes, Seconds

The placeholder for each print has to be an HTML object like: <span>,<div>,<p> etc...
with the id="weeks" bzw. "days" bzw. "hours" bzw. "minutes" bzw. "seconds".

Also you can set the descriptions for each print by Setting the *text vars in the
lines after the date.

Have fun!

*/

var actualdate = new Date();
var todate = new Date(2004,08,01,12,12,12);

var weektext = " Wochen, ";
var daytext = " Tage, ";
var hourtext = " Stunden, ";
var minutetext = " Minuten, ";
var secondtext = " Sekunden.";

var seconds, minutes, hours, days, weeks;
var sh_seconds, sh_minutes, sh_hours, sh_days, sh_weeks;
var remain;

function Countdown() {

actualdate = new Date();
difference = todate - actualdate;
seconds = difference / 1000;

sh_weeks = Math.floor(seconds / 60 / 60 / 24 / 7);
remain = seconds - (sh_weeks * 7 * 24 * 60 * 60);
sh_days = Math.floor(remain / 60 / 60 / 24);
remain = remain - (sh_days * 24 * 60 * 60);
sh_hours = Math.floor(remain / 60 / 60);
remain = remain - (sh_hours * 60 * 60);
sh_minutes = Math.floor(remain / 60);
remain = remain - (sh_minutes * 60);
sh_seconds = Math.floor(remain);

if (document.getElementById('weeks')) { document.getElementById('weeks').innerHTML = sh_weeks + weektext; }
if (document.getElementById('days')) { document.getElementById('days').innerHTML = sh_days + daytext; }
if (document.getElementById('hours')) { document.getElementById('hours').innerHTML = sh_hours + hourtext; }
if (document.getElementById('minutes')) { document.getElementById('minutes').innerHTML = sh_minutes + minutetext; }
if (document.getElementById('seconds')) { document.getElementById('seconds').innerHTML = sh_seconds + secondtext; }
}

function init() {

setInterval("Countdown()",1000);
Countdown();
}

init();

</script>



<span id="weeks"></span>
<span id="days"></span>
<span id="hours"></span>
<span id="minutes"></span>
<span id="seconds"></span>



Demo: http://www.perl-archiv.de/javascript/scripts/javascript_0421_demo.shtml
Title: Re: JavaScript Count down timer.
Post by: roboter88 on September 06, 2004, 12:09:12 PM
kewl i didnt knew there is a fi gooogling


soumi!


cheers :P
Title: Re: JavaScript Count down timer.
Post by: MrCue on September 06, 2004, 12:10:16 PM
yes, there are loads that are just scripts that you enter a date into and they output remaining time.

I want a function, that i can pass a number of seconds into. Its also got to be capable of updating multiple timers.
Title: Re: JavaScript Count down timer.
Post by: Owdy on September 06, 2004, 12:11:54 PM
I dont get it.
Title: Re: JavaScript Count down timer.
Post by: MrCue on September 06, 2004, 12:17:21 PM
I have 2 (but its dynamic) timers on a page

1 week, 1 hour, 36 minutes, 56 seconds
6 days, 21 hours, 47 minutes, 54 seconds

If you refresh the page the numbers are recalculated.

Id like to pass the number of seconds remaining, when the page was created into a javascript function so that it writes the information to the page instead of PHP.
But instead of it just being static text, id like the counters to display the right time remaining (IE, update every second)