Jump to content

Autorefreshing with AJAX while working a cache


ecopetition

Recommended Posts

Hey all,

 

I'm making a little game that contains lots of individual phases - when phase one is complete, it advances to phase two, then three, and so on, until a condition is met that ends the game and starts a new one.

 

I'm using the following jQuery AJAX to refresh the page that the game is played on if a certain condition is met (update() checks if it's time for the next phase, and refresh() does it if is):

 

<script>
    function refresh() {
      $.ajax({
        type: 'POST',
        url: 'index.php',
        data: 'refresh=true',
        timeout: 0,
        success: function(data) {
            $("#current_game").html(data);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          $("#notice_div").html('Error contacting server. Retrying in 60 seconds.');
          window.setTimeout(update, 60000);
        }
    });
    };

    function update() {
      $.ajax({
        type: 'POST',
        url: 'check_time.php',
        data: 'checktime=true',
        timeout: 0,
        success: function(data) {
            $(".time_remaining").html(data);
            window.setTimeout(update, 1000);
            var time = data;
            if(time<=0)
            {
                $(".time_remaining").html("Reloading the page now.");
                $(".time_remaining_end").html("Reloading the page now.");
                refresh();
            }
            else
            {
                $(".time_remaining").html("There are "+data+" seconds remaining in this phase." );
                $(".time_remaining_end").html("There are "+data+" seconds until the next game." );
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          $("#notice_div").html('Error contacting server. Retrying in 60 seconds.');
          window.setTimeout(update, 60000);
        }
    });
    };

$(document).ready(update);
</script>

 

Now, I'm caching the game parameters. The game parameters change each time the phase advances, so I need to delete that cache file, cache_game.php.

 

I was trying to do this by deleting the cache everytime the phase_up() function is called, using the (extremely stripped down for clarity) script (which works with my $cache class):

 

<?php
function phase_up()
{
    global $db, $cache;

    if(condition1)
    {
        ...;
    }
    else
    {
        if(condition2)
        {
            ...;
        }
        else
        {
            ...;

            exit;
        }

        $sql = "UPDATE games
                SET phase_details = ...
                WHERE game_id = ...";
        $db->query($sql);
    }

    $cache->delete('game');
}
?>

 

Yet when my AJAX calls this through index.php, it doesn't advance the phase. Manually refreshing the page does advance the phase.

 

The AJAX just tells index.php to call phase_up().

 

Any help provided would be invaluable, this has been driving me crazy for hours!

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.