Jump to content

Re draw text every second


POC0bob

Recommended Posts

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?

Link to comment
Share on other sites

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 by POC0bob
Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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 by kicken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.