michaelespinosa
michaelespinosa

Reputation: 511

Basic Ajax Cache Issue

I have a single page that I need to on occasion asynchronously check the server to see if the status of the page is current (basically, Live or Offline). You will see I have a function with a var live that is set when the page initially loads. I then do an ajax request to the server to retrieve whether the status of live is true or false. I compare the initial live variable with the newly returned data json object. If they're the same I do nothing, but if there different I apply some css classes. I recursively run it with setTimeout (Is there a better way to recursively do this?).

My Problem: data.live doesn't change from it's initial time it runs even when it has changed in the db. I know my mysql is working because it returs the right value on the initial load. It seems like a caching issue.

Any help is greatly appreciated.

function checkLive() {
    var live = <?=$result["live"]?>;

    $.ajax({
        type: 'get',
        url: '/live/live.php',
        dataType: 'json',

        success: function(data) {
            console.log('checking for updates... current:' + data.live);
            if (data.live == live) {
                return;
            } else {
                var elems = $('div.player_meta, object, h3.offline_message');
                if (data.live == '1') {
                    elems.removeClass('offline').addClass('live');
                } else {
                    elems.addClass('live').addClass('offline');
                }
            }
        }
    });

    setTimeout(function() { checkLive() } ,15000);
} 

checkLive();

Upvotes: 0

Views: 1193

Answers (3)

regilero
regilero

Reputation: 30556

I think Nick Craver has the right response.

For the other point of the question which is you SetTimeout , you could use SetInterval() and avoid the recursive call. But in fact I would stay with a setTimeout() and add a factor on the 15000 time. set that factor as a parameter of checklive. Then you will have a check which will be delayed progressively in time. This will avoid a LOT of HTTp requests from the guy which his still on your page since 48 hours.

Chances are that most of the time users will check for new pages in a regular manner, but someone staying for a very long time on a page is maybe not really there. Here's a piece of code I had doing that stuff.

function checkLive(settings) {
    (...) //HERE ajax STUFF
    setTimeout(function() {
        if ( (settings.reload <2000000000) && (settings.growingfactor > 1) ) {
            checkLive(settings);
            settings = jQuery.extend(settings,{reload:parseInt(settings.reload*settings.growingfactor,10)});
        }
    },settings.reload);
}

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630627

Use the cache option of $.ajax() to append a cache breaker to the URL, like this:

$.ajax({
    type: 'get',
    url: '/live/live.php',
    dataType: 'json',
    cache: false,
    //success, etc.
});

If that doesn't resolve it...look at firebug, see if a request is being made (it should be after this for sure), if it's still getting an old value, the issue is in PHP, not JavaScript.


Unrelated to the issue, just a side tip: If you need no parameters, you can skip the anonymous function call, this:

setTimeout(function() { checkLive() } ,15000);

can just be:

setTimeout(checkLive, 15000);

Upvotes: 5

Greg
Greg

Reputation: 1426

You can check if it's a caching issue by adding unique ID to the url:

change url: '/live/live.php', to url: '/live/live.php?'+new Date().getTime(),

Cheers

G.

Upvotes: 0

Related Questions