Jump to content

Countdown timer - Noob!!!


master82

Recommended Posts

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 :)
Link to comment
Share on other sites

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

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

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.mozilla.org/en/docs/DOM:window.setTimeout
http://developer.mozilla.org/en/docs/DOM:window.setInterval
Link to comment
Share on other sites

Try this: [code]< 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>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.