我正在尝试在我的网站上实现类似Comet的行为来模拟PUSH事件。
我正在使用jQuery和$ .ajax请求。我的原则的问题是,当我获得成功事件时,我会重新请求...但是......我在发送请求时立即获得成功,而不是在执行脚本时完成...
您可能会看到我现在遇到的问题。即使没有来自脚本的数据,也会发生重新请求 - 非常糟糕的行为,因为它调用了我的脚本>每秒50次......
有没有办法知道脚本何时完成(真正的成功事件)?
BR, 斯捷潘。
编辑:代码
/*
IPR Comet implementation
*/
// Define singleton
IPRComet = new function()
{
// Variables
this.m_bStopExecution = false;
this.m_iTmp = 1;
// Init IPR Comet implementation
this.Init = function()
{
// Start poll worker thread
this.m_bStopExecution = false;
this.PollThread();
};
// Main poll thred
this.PollThread = function()
{
if(this.m_bStopExecution)
return;
this.m_iTmp += 1;
$.ajax(
{
url: BASE_IPRBASE_URL + "/AJAX/Comet.php",
success: function(pCometResponse) { IPRComet.OnResponse(pCometResponse); },
error: function() { IPRComet.OnSystemError(); },
complete: function(jqXHR, sStatus)
{
$("#Sandbox-Output2").text(jqXHR.readyState + ": " + IPRComet.m_iTmp);
if(sStatus == "success" || sStatus == "timeout")
IPRComet.PollThread();
},
dataType: "json",
timeout: 30000
});
};
// System error handler
this.OnSystemError = function()
{
// Stop further execution
this.m_bStopExecution = false;
IPRClientHooks.CallHook("Comet::OnSystemError");
};
// Response error handler
this.OnResponseError = function(pCometResponse)
{
// Stop further execution
this.m_bStopExecution = false;
IPRClientHooks.CallHook("Comet::OnResponseError", pCometResponse);
};
// Response handler
this.OnResponse = function(pCometResponse)
{
// Error case
if(pCometResponse.Type == "Error")
{
IPRComet.OnResponseError(pCometResponse);
return;
}
IPRClientHooks.CallHook("Comet::OnResponse", pCometResponse);
};
};
// Start Comet when document is ready
$(document).ready(function() { IPRComet.Init(); });
答案 0 :(得分:0)
不得不从图片中删除jQuery ......它不能正常运行......
必须使用XMLHTTPRequest对象和onreadystatechange事件。
无论如何,这是工作代码:
/*
IPR Comet implementation
*/
// Define singleton
IPRComet = new function()
{
// Variables
this.XHR = null;
this.m_bStopExecution = false;
// Init IPR Comet implementation
this.Init = function()
{
this.m_bStopExecution = false;
this.XHR = new $.ajaxSettings.xhr();
this.XHR.onreadystatechange = function()
{
if(this.readyState == 4 && this.status == 200)
IPRComet.OnResponse(this.responseText);
};
// Start poll worker thread
this.PollThread();
};
// Main poll thred
this.PollThread = function()
{
if(this.m_bStopExecution)
return;
try
{
this.XHR.open("GET", BASE_IPRBASE_URL + "/AJAX/Comet.php", true);
this.XHR.send();
}
catch(pException)
{
this.OnSystemError();
}
};
// System error handler
this.OnSystemError = function()
{
// Stop further execution
this.m_bStopExecution = false;
IPRClientHooks.CallHook("Comet::OnSystemError");
};
// Response error handler
this.OnResponseError = function(pCometResponse)
{
// Stop further execution
this.m_bStopExecution = false;
IPRClientHooks.CallHook("Comet::OnResponseError", pCometResponse);
};
// Response handler
this.OnResponse = function(sCometResponse)
{
var pCometResponse = JSON.parse(sCometResponse);
// Error case
if(pCometResponse.Type == "Error")
{
IPRComet.OnResponseError(pCometResponse);
return;
}
IPRClientHooks.CallHook("Comet::OnResponse", pCometResponse);
this.PollThread();
};
};
// Start Comet when document is ready
$(document).ready(function() { IPRComet.Init(); });