【问题标题】:Ajax/jQuery Abort on timeoutAjax/jQuery 超时中止
【发布时间】:2013-08-26 23:58:55
【问题描述】:

新手 - 问题 -

试图解决的问题:如果有一些打开的连接没有被 XMLHttprequest 关闭,我们想中止它们。

在有 Ajax 调用的 javascript 文件中,我需要为每个调用启动一个计时器,并在成功和失败回调中清除计时器。想要确保 - 如果计时器触发,则意味着请求尚未返回,因此应该中止。对于尽管中止的情况,请求可能会返回 - 想要丢弃它们。想要添加一个标志,在处理之前检查成功和失败回调,并适当处理。

想像这样添加一个计时器:

    var timer = setTimeout(function() {         // Start a timer. If triggered,
                           timedout = true; // set a flag and then
                           request.abort(); // abort the request.
                       },
                       timeout);            // time specified

但是在下面给出的现有代码中,我发现很难理解从哪里启动计时器和中止。

        this.ajaxCall = function(urlSuffix, type, data, successCallback, errorCallback, callbackParam, logFlag)
 {
    if (!successCallback) {
        throw "A successCallback must be supplied";
    }
    if (!errorCallback) {
        throw "An errorCallback must be supplied";
    }
    var st = 0;
    if (logFlag !== false)
        st = LogMessageTestCL(urlSuffix + " - start: " + type, -1);
    $.ajax({
        url: urlRoot + urlSuffix,
        type: type,
        data: data,
        cache: false,

        success: function(msg) {
            try {
                if (logFlag !== false)
                    LogMessageTestCL(urlSuffix + " - end: " + type, st);
                successCallback(msg, callbackParam);
            }
            catch (e) {
                // this will only fail when the calling page is unloaded before the call returns, so no need to log this
            }
        },
        error: function(xhr,status) { 
            try {
                if (logFlag !== false)
                    LogMessageTestCL(urlSuffix + " - error end: " + type, st);
                if (status === "timeout") {
                    alert("30 sec time out");   
                    LogMessageTestCL("callback timed out");
                    return;
                }
                else if (status === "error") {
                    if (xhr.status != 200) {
                        errorCallback(xhr, callbackParam);
                    }
                }
                else
                    errorCallback(xhr, callbackParam);
            }
            catch (e) {
                // this will only fail when the calling page is unloaded before the call returns, so no need to log this
            }
        }
    });
}

非常感谢任何帮助。

【问题讨论】:

    标签: jquery ajax abort


    【解决方案1】:

    不需要自己实现逻辑,超时选项由jquery提供,看jQuery.ajax()的文档有一个选项叫timeout。

    例如:

    $.ajax({
        url: '',
        type: '',
        data: '',
        timeout: 30000, //time in milliseconds - 30000 equals 30 seconds,
        ......
    })
    

    【讨论】:

    • 感谢 Arun 的回复。在 JQuery 给出的超时之上——我们确实想添加这个中止。有没有办法明确地这样做?
    • @user1834443 abort 是什么意思?那么应该发生什么
    • 我的意思是——中止请求——比如 xhr.abort()——你认为这没有必要吗?
    • @user1834443 所以你不想在超时错误的情况下调用失败回调
    • 我想为成功和失败回调启动并清除计时器。如果计时器触发,则表示请求尚未返回,在这种情况下中止请求。
    猜你喜欢
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 2017-01-05
    • 2017-09-08
    • 2011-07-10
    • 2011-03-24
    相关资源
    最近更新 更多