Page 1 of 1

Timing Building Glitch

Posted: Thu Dec 06, 2012 1:47 pm
by vitinho444
Hello guys, some time ago i needed a timer in my building system in my browser based game.

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>

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

Code: Select all

window.location.reload();
because if i comment it i just need to f5 and it works..

Re: Timing Building Glitch

Posted: Thu Dec 06, 2012 10:44 pm
by overras
Thank you man!!

Re: Timing Building Glitch

Posted: Fri Dec 07, 2012 7:30 am
by Winawer
Try

Code: Select all

<?php
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 $currentTime; ?>,
        futureTime = <?php echo $futureTime; ?>,
        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 > 0 ? days + 'd:' : '' ) + ( hours > 0 ? hours + 'h:' : '' ) + minutes + 'min:' + seconds + 's';
    }

    if( currentTime < futureTime ) {
        setInterval( function()
        {
            currentTime++;
            timer.innerHTML = timeLeft( currentTime, futureTime );
        }, 1000 ); // loop every second
    }
</script>


Re: Timing Building Glitch

Posted: Fri Dec 07, 2012 1:58 pm
by vitinho444
overras wrote:Thank you man!!
No problem ;)
Winawer wrote:Try

Code: Select all

<?php
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 $currentTime; ?>,
        futureTime = <?php echo $futureTime; ?>,
        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 > 0 ? days + 'd:' : '' ) + ( hours > 0 ? hours + 'h:' : '' ) + minutes + 'min:' + seconds + 's';
    }

    if( currentTime < futureTime ) {
        setInterval( function()
        {
            currentTime++;
            timer.innerHTML = timeLeft( currentTime, futureTime );
        }, 1000 ); // loop every second
    }
</script>


Same thing.. Refreshing forever..

Re: Timing Building Glitch

Posted: Mon Dec 10, 2012 6:49 am
by Winawer
That means the bug is probably in $futureTime

Re: Timing Building Glitch

Posted: Mon Dec 10, 2012 2:14 pm
by vitinho444
The future time is the time when the building ends. I echo'ed it and its good.

just dont see where the error is..

To display the timer i got:

Code: Select all

if(!$tempoc)
        	{
                	//Present building options and buttons         	
        	}
else
{
       //Player is building
$tempo = time();
        		$tempoc = $aldeia_info['tempoc'];
        		if($tempo >= $tempoc) //if the current time is bigger or equal to the time when the building is done
        		{
        			$building = $aldeia_info['building'];
        			//Here i add one to the level of the building in the village database
                }
                else
                {
                     $currentTime = time();
					$futureTime = $aldeia_info['tempoc'];
					echo $futureTime . " ! " . $currentTime; //debug purposes and its all correct..
					echo '<h4 id="timer"></h4>';

<script type="text/javascript">
					var currentTime = <?php echo $currentTime; ?>,
						futureTime = <?php echo $futureTime; ?>,
						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 > 0 ? days + 'd:' : '' ) + ( hours > 0 ? hours + 'h:' : '' ) + minutes + 'min:' + seconds + 's';
					}
				
					if( currentTime < futureTime ) {
						setInterval( function()
						{
							currentTime++;
							timer.innerHTML = timeLeft( currentTime, futureTime );
						}, 1000 ); // loop every second
					}
					</script>
                  }

}

What can possibly be wrong?

Re: Timing Building Glitch

Posted: Mon Dec 10, 2012 10:43 pm
by Callan S.

Code: Select all

               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();
                  }
            
               }
What sets 'end' to a value higher than zero?

It seems that when it gets to zero, unless something then makes it higher than zero it will refresh over and over?

Re: Timing Building Glitch

Posted: Tue Dec 11, 2012 6:55 am
by Winawer
vitinho444 wrote:The future time is the time when the building ends. I echo'ed it and its good.

just dont see where the error is..

To display the timer i got:

Code: Select all

if(!$tempoc)
        	{
                	//Present building options and buttons         	
        	}
else
{
       //Player is building
$tempo = time();
        		$tempoc = $aldeia_info['tempoc'];
        		if($tempo >= $tempoc) //if the current time is bigger or equal to the time when the building is done
        		{
        			$building = $aldeia_info['building'];
        			//Here i add one to the level of the building in the village database
                }
                else
                {
                     $currentTime = time();
					$futureTime = $aldeia_info['tempoc'];
					echo $futureTime . " ! " . $currentTime; //debug purposes and its all correct..
					echo '<h4 id="timer"></h4>';

<script type="text/javascript">
					var currentTime = <?php echo $currentTime; ?>,
						futureTime = <?php echo $futureTime; ?>,
						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 > 0 ? days + 'd:' : '' ) + ( hours > 0 ? hours + 'h:' : '' ) + minutes + 'min:' + seconds + 's';
					}
				
					if( currentTime < futureTime ) {
						setInterval( function()
						{
							currentTime++;
							timer.innerHTML = timeLeft( currentTime, futureTime );
						}, 1000 ); // loop every second
					}
					</script>
                  }

}

What can possibly be wrong?
What's happening when $tempo >= $tempoc?

Re: Timing Building Glitch

Posted: Tue Dec 11, 2012 12:09 pm
by vitinho444
@Callan

What do you mean, i should put if(left > 1) ?

@Winawer
tempo it's the actual time();
the tempoc is the time() + buildingtime = finaltime

so when tempo >= tempoc it means the actual time is bigger or equal to the building final time, so the building is over.

Re: Timing Building Glitch

Posted: Tue Dec 11, 2012 2:09 pm
by Winawer
No I mean post the code.