【问题标题】:jQuery $.ajax and Comet methodology - how to know when the script has done its execution?jQuery $.ajax 和 Comet 方法 - 如何知道脚本何时执行完毕?
【发布时间】:2013-01-27 20:58:59
【问题描述】:

我正在尝试在我的网站上实现类似 Comet 的行为来模拟 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(); });

【问题讨论】:

  • 您是否尝试过使用successerror 回调来调用IPRComet.PollThread(); 而不是complete 回调?还应该查看 $.when api.jquery.com/jQuery.when 中的 AJAX 示例
  • 是的,我有。 complete 同时触发成功和错误。成功是完成事件,但过滤到状态消息“成功”。 :(
  • success 仅在服务器端代码完成后调用,所以我同意@charlifi 的评论。也许您需要设置 async=false 属性。
  • async=false 没有达到目的。我正在尝试使用 AJAX 实现类似推送的行为。 async 设置为 false 会阻止我的其他脚本执行...我需要类似 PUSH 的功能来实时用户通知。 (如 Facebook)
  • 您的客户端行为是正确的。挑战在于服务器端,要实现一种机制,仅在需要报告一些状态变化时做出响应。

标签: jquery ajax comet long-polling


【解决方案1】:

不得不从图片中删除 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(); });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 2012-02-09
    • 2016-09-25
    • 1970-01-01
    相关资源
    最近更新 更多