0

我正在尝试在我的网站上实现类似彗星的行为来模拟 PUSH 事件。

我正在使用 jQuery 和 $.ajax 请求。我的原则的问题是当我收到成功事件时我会重新请求......但是......我在发送请求时立即获得成功,而不是在脚本完成执行时......

你可能会看到我现在遇到的问题。即使脚本中没有数据,也会发生重新请求 - 非常糟糕的行为,因为它每秒调用我的脚本 > 50 次......

有没有办法知道脚本何时完成了它的部分(真正的成功事件)?

兄弟,斯捷潘。

编辑:代码

/*

    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(); });
4

1 回答 1

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(); });
于 2013-01-27T23:11:54.073 回答