Php javascript window.setInterval在最新的Firefox和IE9上不工作
我最近为个人需要创建了一个自定义ping框(聊天)。当我在Firefox 3.6.13中编码和测试时,它运行良好。然而,与window.setInterval相关的功能在IE9或Firefox6中似乎不起作用 下面是javascript的代码Php javascript window.setInterval在最新的Firefox和IE9上不工作,php,javascript,jquery,Php,Javascript,Jquery,我最近为个人需要创建了一个自定义ping框(聊天)。当我在Firefox 3.6.13中编码和测试时,它运行良好。然而,与window.setInterval相关的功能在IE9或Firefox6中似乎不起作用 下面是javascript的代码 <script> function loadNewPosts(){ var id = $(".altbgcolor:first").attr("id"); $.get('/updatepin
<script>
function loadNewPosts(){
var id = $(".altbgcolor:first").attr("id");
$.get('/updateping.php', { updateid: id , }, function(data){
$(".newslist").prepend(data);
}, 'html');
}
window.setInterval(loadNewPosts, 1000*3)
$(document).ready(function() {
// bind form using ajaxForm
$("#pingForm").validate({
submitHandler: function(form) {
$('#pingForm').ajaxSubmit({
// target identifies the element(s) to update with the server response
target: '#pingResult',
// success identifies the function to invoke when the server response
// has been received; here we apply a fade-in effect to the new content
success: function() {
$('#msg').val('');
$('#pingResult').fadeIn('slow');
$('#pingResult').fadeOut(2000);
}
});
}
});
})
</script>
函数loadNewPosts(){
var id=$(“.altbgcolor:first”).attr(“id”);
$.get('/updateping.php',{updateid:id,},函数(数据){
$(“.newslist”).prepend(数据);
}","html";;
}
window.setInterval(loadNewPosts,1000*3)
$(文档).ready(函数(){
//使用ajaxForm绑定表单
$(“#pingForm”).validate({
submitHandler:函数(形式){
$('#pingForm').ajaxSubmit({
//目标标识要使用服务器响应更新的元素
目标:“#pingResult”,
//success标识服务器响应时要调用的函数
//已收到;这里我们对新内容应用淡入效果
成功:函数(){
$('#msg').val('');
$('pingResult').fadeIn('slow');
$('pingResult')。淡出(2000年);
}
});
}
});
})
下面是HTML
<ul class="newslist" style="width:630px;">
<li class="altbgcolor" id=64>
<div>
<div class="newsthumb" style="width:50; height:50; "center center no-repeat;"><img src="images/personal/sunny.jpg" /></div>
<div class="newstext" style="margin:0px;">
<h1 style="color:#081C28;"><img width="11" height="9" src="/content/icon_topic_newest.gif">how r u?</h1>
</div>
<br /><br />
<div style="font-size: 0.6em; color:#666666;">
<span style="text-decoration:none; color:none; padding:5px;"><i> from: <a href="" style="text-decoration: none;color:#105289 ;" onmouseover="this.style.textDecoration = 'underline'" onmouseout="this.style.textDecoration = 'none'">Sunny</a></i></span>
<span style="text-decoration:none; color:none; padding:5px; "><i> posted: <a href="" style="text-decoration: none;color:#105289 ;" onmouseover="this.style.textDecoration = 'underline'" onmouseout="this.style.textDecoration = 'none'">October 29, 2011, 9:58 am</a></i></span>
</div>
<div class="clear"></div>
</div>
</li>
</ul>
-
要么在get请求中添加时间戳,要么改用post
$.get('/updateping.php', { updateid: id , time: new Date().valueOf() }, function(data){
...
或
要么在get请求中添加时间戳,要么改用post
$.get('/updateping.php', { updateid: id , time: new Date().valueOf() }, function(data){
...
或
因为您已经在使用jQuery,所以可以尝试以下方法:
$(document).ready(function() {
$.ajaxSetup({ cache: false });
});
根据jQuery文档,jQuery随后会自动向您的请求追加一个时间戳,这样浏览器就不会试图在后续请求中使用缓存
或者,您可以尝试使用HTTP响应头的“缓存控制”部分中的设置在服务器端解决缓存问题,但我不知道如何在PHP中解决此问题。由于您已经在使用jQuery,您可以尝试以下方法:
$(document).ready(function() {
$.ajaxSetup({ cache: false });
});
根据jQuery文档,jQuery随后会自动向您的请求追加一个时间戳,这样浏览器就不会试图在后续请求中使用缓存
或者您可以尝试使用HTTP响应头的“缓存控制”部分中的设置修复服务器端的缓存问题,但我不知道如何在PHP中执行此操作。如果在$.get()的回调函数中使用console.log(data)
或alert(data)
,您会得到什么?(另外,如果您已经确认每三秒调用一次PHP,那么这可能不会导致问题,但是在对象{updateid:id,}
-一些浏览器允许使用尾随逗号,一些浏览器不允许使用尾随逗号)我删除了尾随逗号,但没有成功:((尽管找到得不错;)是的,php文件每3秒调用一次,我也在IE9中检查了tht,但是奇怪的是,php的o/p没有显示在主页上。:((但是您是否尝试了console.log(数据)
和/或警报(数据)
?不完全正确,但阅读我上面的更新,我想我得到了一些东西…但不确定…顺便说一句,你想让我在loadNewPosts()函数中用alert()替换$.get()?不,不要替换$.get()
。我的意思是临时放置警报(数据)
或控制台.log(数据)
,而不是(或之前)你有$(.newslist)的地方.prepend(data);
-仅用于调试目的,查看在您尝试使用数据时,数据设置为什么。并确认实际调用了回调函数。但看起来您已经将其缩小到缓存问题,因此现在不需要警报/console.log如果console.log,您会得到什么(数据)
或警报(数据)
在$.get()
的回调函数中(另外,如果您确认每三秒调用一次PHP,这可能不会导致问题,但在对象{updateid:id,}中有一个尾随逗号)
-有些浏览器允许使用尾随逗号,有些浏览器不允许。)我删除了尾随逗号,但没有成功:((很好的发现;)。是的,php文件每3秒调用一次,我也在IE9中检查了tht,但奇怪的是,php的o/p没有显示在主页上。((但是您尝试了console.log(数据)了吗)
和/或警报(数据)
?不完全正确,但阅读我上面的更新,我想我得到了一些东西…但不确定…顺便说一句,你想让我在loadNewPosts()函数中将$.get()替换为alert()?不,不要替换$.get()
。我的意思是临时将警报(数据)
或控制台.log(数据)
替换为(或之前)其中有$(“.newslist”).prepend(数据)
-只是为了调试目的,查看在您尝试使用数据时设置了什么数据。并确认回调函数确实被调用。但看起来您已经将其缩小到缓存问题,因此现在不需要警报/控制台。谢谢gilly3,它起作用了。我尝试了$.post()方法。但是我不太明白这失败的技术原因。你能解释一下吗?浏览器可能会缓存GET
请求,但它们从不缓存POST
请求。在GET
请求的末尾添加时间戳会导致URL是唯一的,从而绕过浏览器缓存。感谢gilly3,它成功了。我尝试了$.post()方法。但是我真的不明白这失败的技术原因。你能解释一下吗?浏览器可能会缓存GET
请求,但它们从不缓存post
请求。在GET
请求的末尾添加时间戳会导致