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!

 

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.