在我的网站上,我使用了一个长轮询jQuery ajax函数。此函数中的这个ajax调用有50秒超时,然后再次运行,等待服务器上的更改。检查ajax调用/ http连接是否为当前活动
但是,有时长轮询http连接停止。例如,当互联网连接关闭或客户端电脑在休眠或休眠后打开时发生这种情况。问题在于“完成”回调没有发生,$ .ajax()函数仍在等待超时,而http连接不再存在。
如果连接仍处于打开状态,如何检查jquery/javascript?
这是我的长轮询脚本:
function longPoller() {
$.ajax({
type: "GET",
url: '/longpolltest2.php',
dataType: 'json',
async: true,
cache: false,
timeout:50000, /* Timeout in ms */
success: function(data){
alert('success');
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert('error');
},
complete: function() {
alert('complete');
longPoller(); // restart long poller request
}
});
}
@jAndy,我已经更新了剧本,但我收到以下错误:
权限遭拒,http://domain.com为对象创建包装类UnnamedClass
function masterLongPollerDebugger() {
xhr = $.ajax({
type: "GET",
url: '/longpolltest2.php',
dataType: 'json',
async: true,
cache: false,
timeout:50000,
success: function(data){
alert('success');
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert('error');
},
complete: function() {
alert('complete');
masterLongPollerDebugger();
}
});
}
function ajaxRunning() {
xhr._onreadystatechange = xhr.onreadystatechange; // store a reference
xhr.onreadystatechange = function()
{ // overwrite the handler
xhr._onreadystatechange(); // call the original stored handler
alert(xhr.readyState);
if (xhr.readyState === 3)
alert('Interactive');
};
}
我已更新原始帖子以获取更多信息。我得到一个错误:**拒绝http://domain.com为UnnamedClass类的对象创建包装的权限** – koen 2010-09-15 10:33:08