I am in need of help. I am making a count down script for my game website which grabs the end time from the database and counts down until the current time is equal to the database time. But I can not seem to get it to stop when it hits 0. I have looked into setTimeout() and I do not understand how I would get this to work.
Also when the page is refreshed it starts the timer again from the database time. How would i either, update the database to the time left or stop the timer from refreshing.
Thanks in adavance. Below is the script I have so far
<?php
include "header.php";
?>
<html>
<head>
<script type="text/javascript">
var curTime = Math.floor( (new Date()).getTime() / 1000 ); // replaced with PHP time(), presumably
var endTime = Math.floor( (new Date()).getTime() / 1000 ) + <?php echo $user_class->jail;?>;
function tick( )
{
var secs = endTime - cTime;
var mins = Math.floor( secs / 60 );
secs %= 60;
document.getElementById("ticker").innerHTML =
mins + " minutes, " + secs + " seconds";
++curTime;
}
</script>
</head>
<body onload="setInterval(tick,1000); tick();">
<h3>Time remaining is <span id="ticker"></span></h3>
</body>
</html>
My database is currently updated every 1 minute through a cron so it can be out due to the cron. But I was wondering if there was a way to update the database through the countdown instead so it will be acurate as other the timer