www.webdeveloper.com
Results 1 to 2 of 2

Thread: Countdown time with update

  1. #1
    Join Date
    Sep 2012
    Posts
    2

    Countdown time with update

    Hello!

    I am trying to make a script that counts down from 9 minutes. This part is done.

    Now, when the timer hits 0 I want 2 things to happen:
    1. Adds +1 to a specific table in my SQL db which is named "turns"
    2. Start the timer over from 9 IF turns < 320


    In my version I have the countdown from 9 minutes in minutes and seconds. I also have the time to maximum turns. Lets say you have 300 turns and maximum turns is 320 then it will take 3 hours until you got max.

    Here is how my code looks like:



    HTML Code:
    <?php
    include ('include.inc.php');
    $maxTurns = 320;
    
    //$t=gmmktime();
    $currentTime = gmmktime();
    $targetTime = $nextTurnTime+540;
    echo $currentTime."<br />";
    echo(date("D F d Y H:i:s",$currentTime));
    echo "<br /><br />".$targetTime."<br />";
    echo(date("D F d Y H:i:s",$targetTime));
    echo "<br /><br />";
    echo $targetTime-$currentTime."<br /><br />";
    $countDownSec =  $targetTime-$currentTime;
    ?>
    
    
    
    
    
    <html>
    	<body onload="countDown()">
    		<div id="turns"></div>
    		<div id="nextTurn"></div>
    		<div id="maxTurns"></div>
    		
    	</body>
    		
    	<script type="text/javascript"> 
    	
    	var intTurnCountDown = <?php echo $countDownSec; ?>;
    	var intTurns = <?php echo $turns+1; ?>;
    	var intMaxTurns = <?php echo $maxTurns; ?>;
    	var intMaxTurnsCountDown = (intMaxTurns - (intTurns-1))*intTurnCountDown;
    	var updateDb = <?php echo $mysql->myQuery("UPDATE `accounts` SET city_name='test' WHERE login_name='Lejjo'"); ?>
    	
    	
    	
    	function countDown()
    	{
    		if(intTurnCountDown <= 0)
    		{
    			if(intTurns == intMaxTurns)
    			{
    				nextTurn.innerText = 'Done';
    				maxTurns.innerText = 'Done';
    				turns.innerText = intTurns++ + ' turns remaining';
    				return;
    			}
    			turns.innerText = intTurns++;
    			intTurnCountDown = <?php echo $nextTurnTime; ?>;
    			
    		}
    		
    		
    		turns.innerText = intTurns-1 + ' turns remaining.';
    		
    		
    		
    		var nextTurnsSec = intTurnCountDown--;
    		var nextTurnMinutes = Math.floor(nextTurnsSec/60);
    		var nextTurnSeconds = Math.floor(nextTurnsSec-nextTurnMinutes*60);
    		
    		if(nextTurnMinutes > 0)
    			nextTurn.innerText = 'Next turn in ' + nextTurnMinutes + ' minutes and ' + nextTurnSeconds + ' seconds.';
    		else
    			nextTurn.innerText = 'Next turn in ' + nextTurnSeconds + ' seconds.';
    		
    		
    		
    		var maxTurnsSec = intMaxTurnsCountDown--;
    		var maxTurnsHours = Math.floor(maxTurnsSec/3600);
    		var maxTurnsMinutes = Math.floor((maxTurnsSec-maxTurnsHours*3600)/60);
    		var maxTurnsSeconds = Math.floor(maxTurnsSec-maxTurnsHours*3600-maxTurnsMinutes*60);
    			
    		if(maxTurnsHours > 0)
    			maxTurns.innerText = 'You will have max turns stored in ' + maxTurnsHours + ' hours, ' + maxTurnsMinutes + ' minutes and ' + maxTurnsSeconds + ' seconds.';
    		else if(maxTurnsHours <= 0 && maxTurnsMinutes > 0)
    			maxTurns.innerText = 'You will have max turns stored in ' + maxTurnsMinutes + ' minutes and ' + maxTurnsSeconds + ' seconds.';
    		else if(maxTurnsHours <= 0 && maxTurnsMinutes <= 0)
    			maxTurns.innerText = 'You will have max turns stored in ' + maxTurnsSeconds + ' seconds.';
    		
    		
    	
    		setTimeout("countDown()",1000);
    	}
    	</script>
    	
    
    </html>
    Legend:
    $turns = how much turns there are stored in db
    $nextTurnTime = same like $currentTime but this value is not assigned here, it is SELECTED from the db








    I hope this is enough information on what I need help with. I have everything done and its works but once it hits 0 I can't figure out how to add +1 to turns and make a new target time so the counter starts from 9 again if I don't have max turns which is 320.

    Thanks in advance!
    Lejjo

  2. #2
    Join Date
    Sep 2012
    Posts
    2
    btw, nvm this line since I used this for test only:

    Code:
    var updateDb = <?php echo $mysql->myQuery("UPDATE `accounts` SET city_name='test' WHERE login_name='Lejjo'"); ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles