Jump to content


Photo

Countdown timer - Noob!!!


  • Please log in to reply
2 replies to this topic

#1 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 30 September 2006 - 05:14 PM

i currently have a php page that has a calculation on it. The calculation looks at a stored (SQL database) unix timestamp and the current time and works out how long it will be to that stored time. Simple, but its static and only updates upon refresh.

So is it possible to create a live countdown to show how long is left from the unix timestamp stored on my SQL database (can be placed into a variable in php)?

And if so, is it possible to set it so that upon reaching the time 00:00:00 to refresh the page?

I've never done any JS before so if anyone could help id be greatful :)

#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 30 September 2006 - 07:22 PM

you could use setTimeout or setInterval to run a script (or call a function) that rewrites a countdown number on a webpage.

<div name='mydiv'>100</div>
< script type='text/javascript'>
var mycount=100;
var my_interval = setInterval("document.getElementById('mydiv').innerHTML = mycount--;",1000);
</script>

That should count down from 100 seconds, but it wouldn't stop at 0. You'll need to beef it up by creating a function that refreshes (or stops) when the counter hits zero. You'd probably also want to write an interpreter that would convert the seconds to days, hours, minutes, and seconds. For example, instead of 10,000 seconds, the visitor would see 2:46:45.

Here's some info on setTimeout and setInterval
http://developer.moz...ndow.setTimeout
http://developer.moz...dow.setInterval

#3 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 01 October 2006 - 05:51 PM

Try this:
< script type="text/javascript">
var start = 10
var now = start
var interval = setInterval('update_countdown()',1000);

function update_countdown()
{
	now = now-1
	if(now >= 0)
	{
		document.getElementById('countdown').innerHTML = now
	}
	else {
		clearInterval(interval)
	}
}
</script>

<div>There is <span id="countdown">10</span> seconds left.</div>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users