使用jQuery定期ajax请求超时 [英] Timeouts on periodic ajax requests with jQuery

查看:71
本文介绍了使用jQuery定期ajax请求超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码每3秒发送一次ajax请求:

i use the following code to send an ajax-request every 3 seconds:

var refreshId = setInterval(function() {
    $.ajax({
        async: true,
        url: 'gohere',
        dataType: 'text',
        cache: false,
        timeout: 5000,

        success: function(result, textStatus, XMLHttpRequest) {
            alert('textStatus: ' + textStatus + ",\nXHR.readyState: " + XMLHttpRequest.readyState + ",\nXHT.status: " + XMLHttpRequest.status);
        },

        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert('textStatus: ' + textStatus + ",\nXHR.readyState: " + XMLHttpRequest.readyState + ",\nXHT.status: " + XMLHttpRequest.status);
        }
    });
}, 3000);

问题是,即使家庭服务器无法访问(例如,未连接到LAN) )将调用success-function而不是带有timeout-status的错误函数(在使用FireFox 3.6.7的Ubuntu 10.04上测试)。

(没有定期请求它将正常工作:在超时时,使用正确的'timeout'-statusText调用错误函数。)

The problem is, that even if the home-server is not accessible (eg not connected to the LAN) the success-function will be called instead of the error-function with a "timeout"-status (tested on Ubuntu 10.04 with FireFox 3.6.7).
(Without periodic requests it will work fine: On timeouts the error-function is called with the correct 'timeout'-statusText.)

success-alert-function将在超时时显示以下文本:

The success-alert-function will show the following text on timeouts:


    textStatus: success,
    XHR.readyState: 4,
    XHR.status: 0


所以我需要使用XHR.status值,成功时应为200,以确定错误。
这是正常行为还是我做错了什么?

So i need to use the XHR.status-value, which should be 200 on success, to determine errors. Is this the normal behaviour or did i something wrong?

推荐答案

思考如何在成功和错误中移动setTimeout而是处理程序,并使其异步递归。

ponder on moving the setTimeout in the success and error handlers instead and make it asynchronously recursive.

var refreshId = function() {
    $.ajax({
        async: true,
        url: 'gohere',
        dataType: 'text',
        cache: false,
        timeout: 5000,

        success: function(result, textStatus, XMLHttpRequest) {
            alert('textStatus: ' + textStatus + ",\nXHR.readyState: " + XMLHttpRequest.readyState + ",\nXHT.status: " + XMLHttpRequest.status);
            setTimeout(refreshId, 3000);
        },

        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert('textStatus: ' + textStatus + ",\nXHR.readyState: " + XMLHttpRequest.readyState + ",\nXHT.status: " + XMLHttpRequest.status);
            setTimeout(refreshId, 3000);
        }
    });
};

事实是,setTimeout和setInterval时间不可靠,特别是对于警报。只要警报对话框保持打开状态,警报就会停止javascript。此外,setTimeout和setInterval不会中断当前运行的代码以保证完美的时间,它们将等到javascript线程在代码执行之间有一个开放和跳转。

Thing is, setTimeout and setInterval time is not reliable, especially with alerts. An alert call stalls javascript as long as the alert dialog remains open. Furthermore, setTimeout and setInterval do not interrupt currently running code to guarantee perfect timing, they will wait till the javascript thread has an opening and jump between code executions.

使用之前的代码设置,你还有机会刷新refreshId调用1的响应将在refreshId调用2之后到达,因为http往返花费了不确定的时间。

With your previous code setup, you also had a chance that the response of refreshId call 1 will arrive after refreshId call 2 due to the http roundtrip taking an undetermined ammount of time.

通过制作你的setTimeout取决于你的响应,你得到一个更稳定和透明的结果。

By making your setTimeout depend on the response comming in, you get a more stable and transparent result.

这篇关于使用jQuery定期ajax请求超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆