master82 Posted September 30, 2006 Share Posted September 30, 2006 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 :) Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted September 30, 2006 Share Posted September 30, 2006 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 setIntervalhttp://developer.mozilla.org/en/docs/DOM:window.setTimeouthttp://developer.mozilla.org/en/docs/DOM:window.setInterval Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 1, 2006 Share Posted October 1, 2006 Try this: [code]< script type="text/javascript">var start = 10var now = startvar 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.