POC0bob Posted January 31, 2013 Share Posted January 31, 2013 Hello, Is it possible to run a piece of PHP code every second? Like what the PHP code does is run a function that does some math on a time difference, then displays it with some text: <?php $gameStarted = gettime($dbtime); list($minutes, $seconds) = minutesSince($gameStarted); ?> <span class="rght">Next turn: <?php if(ceil($minutes/30) > 1){echo("I owe you " . ceil(($minutes/30)-1) . " Energy points."); }else{if($seconds >= 10){$despsec = $seconds;}else{$despsec = ("0".$seconds);} echo (ceil(30 - $minutes) . ":" . $despsec);} ?> But I need it to run that, and re print the number every second. What is, if possible is the best way to do this, and how? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 31, 2013 Share Posted January 31, 2013 Using AJAX. PHP runs on the server, is sent to the client, and displayed in the client (browser). Or better yet, just plain javascript. Quote Link to comment Share on other sites More sharing options...
POC0bob Posted January 31, 2013 Author Share Posted January 31, 2013 I did a google search on that - But I couldn't find anything on how to do it in AJAX. I knew I needed to do it in AJAX but had no luck. My server will be able to handle the php every second, no problem so I am not worried about that. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 31, 2013 Share Posted January 31, 2013 jQuery has a great AJAX utility. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 31, 2013 Share Posted January 31, 2013 Do it all client-side with JS setInterval() http://javascript.about.com/library/blstvsi.htm Quote Link to comment Share on other sites More sharing options...
POC0bob Posted January 31, 2013 Author Share Posted January 31, 2013 (edited) This is what I tried, but it just gives me an error on the last } <script src="code.jquery.com/jquery-1.7.1.min.js"; type="text/javascript"> var statusIntervalId = window.setInterval(update, 1000); function update() { $.ajax({ url: 'updateturns.php', dataType: 'text', } } </script> It doesn't tell me the error or anything, just says syntax error.. Edited January 31, 2013 by POC0bob Quote Link to comment Share on other sites More sharing options...
kicken Posted January 31, 2013 Share Posted January 31, 2013 Your missing a closing parenthesis. function update() { $.ajax({ url: 'updateturns.php', dataType: 'text', }); } Note that trying to make an ajax request to your server every second would likely destroy your server's performance. As recommended earlier, you should just do everything in JS locally on the client end without having to contact the server. Quote Link to comment Share on other sites More sharing options...
POC0bob Posted January 31, 2013 Author Share Posted January 31, 2013 How would I do it in JS? Quote Link to comment Share on other sites More sharing options...
kicken Posted January 31, 2013 Share Posted January 31, 2013 (edited) Assuming this is related to your previous thread about updating something every 30 minutes, a fairly simple approach would be to do something along the lines of this: http://jsfiddle.net/cKeNs/ <script type="text/javascript"> (function(){ var secondsSince = <?php echo secondsSince(); /* Have this return # of seconds since last update */ ?>; function update(){ secondsSince++; var thirtyMinutes = 60*30; if (secondsSince >= thirtyMinutes){ var msg = 'Now'; //Optionally you could kick off an ajax request to the server so it can process the fact that a new turn is available. } else { var minutesRemaining = Math.floor((thirtyMinutes-secondsSince)/60); var secondsRemaining = (thirtyMinutes-secondsSince)%60; var msg = minutesRemaining + ':' + secondsRemaining; } document.getElementById('timeDisplay').innerHTML = msg; setTimeout(update, 1000); } update(); }()); </script> Edited January 31, 2013 by kicken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.