我一直在尝试让这个长轮询脚本工作,但是我一直卡住了。这一次,我的问题出在向服务器发送ajax请求上。提供数据的php脚本(使用循环和sleep运行长轮询)工作得非常好,但我的jQuery脚本却没有。
到目前为止,我已经尝试过使用普通的$.ajax()
请求和$.post()
请求到脚本,但是当我这样做时,整个脚本都会停止。实际上,脚本加载得很好,但页面显示它仍在加载中(使用 Google Chrome 时,它在屏幕底部显示“正在等待”栏)。在 php 脚本的 60 秒超时后,jQuery 脚本收到“false”,因为没有找到任何数据。然后脚本应该重新请求数据。
我的问题是:如何避免浏览器显示它正在后台加载某些内容?我的服务器是共享服务器,因此当涉及安装服务器端东西时,我没有权限,所以必须仅使用 JavaScript(jQuery)和 php 来解决这个问题。
编辑:刚在 Internet Explorer 中检查了我的脚本,似乎没有显示任何正在加载脚本的迹象,但在 Google Chrome 中仍然存在问题。
编辑 发布一些代码:
这是我的 javascript 文件:
if($("#interestboard"))
{
var lasttime = $("#lasttime").val();
var pollurl = $("#pollurl").val();
lpStart();
}
function lpStart()
{
$.ajax({
type: "GET",
url: pollurl+lasttime,
async: true,
cache: false,
success:function(data)
{
alert(data);
},
error: function()
{
alert("Something went wrong");
}
});
}
当然,这段代码被包含在
$(document).ready()
中。我的php脚本大致如下:
while(time()-$time<60)
{
$this->class = $class["id"];
$this->orderBy("timestamp");
$this->_extraConditions = "`timestamp`>'$lastTime' AND ";
$result = $this->search();
if($result)
return $result;
usleep(250000);
clearstatcache();
}
使用Firebug(在Google Chrome中)时,它仅显示已发出请求,但未在控制台中显示常规的GET字符串末尾的加载图标。在Firefox中,它也只显示正在等待请求(显示加载图标),但至少浏览器不会像页面正在加载一样进行操作。
谢谢...