Timing Building Glitch

C++, C#, Java, PHP, ect...
Post Reply
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Timing Building Glitch

Post 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..
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
overras
Posts: 91
Joined: Sun Oct 03, 2010 7:25 am

Re: Timing Building Glitch

Post by overras »

Thank you man!!
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Timing Building Glitch

Post 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>

User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Timing Building Glitch

Post 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..
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Timing Building Glitch

Post by Winawer »

That means the bug is probably in $futureTime
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Timing Building Glitch

Post 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?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Timing Building Glitch

Post 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?
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Timing Building Glitch

Post 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?
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Timing Building Glitch

Post 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.
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Timing Building Glitch

Post by Winawer »

No I mean post the code.
Post Reply

Return to “Coding”