基本的Ajax缓存问题 [英] Basic Ajax Cache Issue
问题描述
我有我需要有时异步检查服务器,查看是否页面的状态为当前(基本,Live或离线)单页。你会看到我有当页面加载最初被设定VAR直播功能。然后,我做一个AJAX请求到服务器以检索实时的状态是否是真还是假。我最初的生存期变量与新返回的数据JSON对象进行比较。如果它们是相同的我什么也不做,但如果有不同的我申请一些CSS类。我递归与setTimeout的运行(有没有更好的方法来递归地做到这一点?)。
我的问题: data.live不会改变从它的运行,即使它在数据库中已更改初始时间。我知道我的MySQL的工作,因为它returs在初始加载正确的价值。这似乎是一个缓存的问题。
任何帮助是极大的AP preciated。
函数checkLive(){
VAR活= LT;??= $结果[活]取代;
$阿贾克斯({
输入:'得到',
网址:/live/live.php,
数据类型:JSON,
成功:功能(数据){
的console.log(检查更新...电流:'+ data.live);
如果(data.live ==住){
返回;
} 其他 {
VAR elems = $('div.player_meta,对象,h3.offline_message');
如果(data.live =='1'){
。elems.removeClass(离线)addClass('活');
} 其他 {
。elems.addClass('活')addClass(离线);
}
}
}
});
的setTimeout(函数(){checkLive()},15000);
}
checkLive();
使用 缓存
$选项阿贾克斯()
附加缓存断路器的URL,这样的:
$。阿贾克斯({
输入:'得到',
网址:/live/live.php,
数据类型:JSON,
缓存:假的,
//成功,等等。
});
如果不解决这个问题......看萤火,是否正在做一个请求(它应该是在此之后肯定的),如果它的还是的得到一个旧值时,问题是在PHP,JavaScript不。
无关的问题,只是一个侧面提示:如果您需要任何参数,则可以跳过匿名函数调用,这样的:
的setTimeout(函数(){checkLive()},15000);
可以只是:
的setTimeout(checkLive,15000);
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();
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);
这篇关于基本的Ajax缓存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!