Basic Ajax Cache Issue

前端 未结 3 1032
感情败类
感情败类 2020-12-07 06:48

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

相关标签:
3条回答
  • 2020-12-07 07:13

    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);
    
    0 讨论(0)
  • 2020-12-07 07:13

    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);
    }
    
    0 讨论(0)
  • 2020-12-07 07:20

    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.

    0 讨论(0)
提交回复
热议问题