I'm trying to implement Comet-like behaviour on my site to simulate PUSH events.
I'm using jQuery and $.ajax request. The problem with my principle is that I make re-request when I get success event... But... I get success immediately when the request is sent, not when the script is done with the execution...
You might see the problem I now have. The re-request happens even when there is no data from the script - very bad behaviour because it calls my script > 50 times per second...
Is there a way to know when the script has done its part (real success event)?
Br, Stjepan.
EDIT: Code
/*
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(); });