Timing Building Glitch
Posted: Thu Dec 06, 2012 1:47 pm
Hello guys, some time ago i needed a timer in my building system in my browser based game.
I made like this:
So as you can see, the page is gathering the time and then decrease it, in the end reloads the page and it should be working..
It was working until now that when it reaches 0, it reloads forever..
I know the error is in
because if i comment it i just need to f5 and it works..
I made like this:
Code: Select all
echo '<h4><center>' . $lang['Building'][$language] . "</h4>";
$currentTime = time();
$futureTime = $aldeia_info['tempoc'];
echo '<h4 id="timer"></h4>';
<script type="text/javascript">
var currentTime = <?php echo time(); ?>,
futureTime = <?php echo $tempoc; ?>,
timer = document.getElementById('timer');
function timeLeft(current, end)
{
var days = 0,
hours = 0,
minutes = 0,
seconds = 0,
left = end - current;
if( left > 0 )
{
days = Math.floor(left / 86400);
left -= days*86400;
hours = Math.floor(left / 3600);
left -= hours*3600;
minutes = Math.floor(left/60);
seconds = left - minutes * 60;
}
else
{
window.location.reload();
}
//return days + ':' + hours + ':' + minutes + ':' + seconds;
//return minutes + ':' + seconds;
return ( days > 0 ? days + 'd:' : '' ) + ( hours > 0 ? hours + 'h:' : '' ) + minutes + 'min:' + seconds + 's';
}
setInterval( function()
{
currentTime++;
timer.innerHTML = timeLeft(currentTime, futureTime);
}, 1000 ); // loop every second
</script>
It was working until now that when it reaches 0, it reloads forever..
I know the error is in
Code: Select all
window.location.reload();