Ajax / jQuery中止超时

Newbie - Quesiton -

Problem trying to solve: In case there are some open connections that aren't closed by XMLHttprequest, we want to abort them.

In a javascript file, where there are Ajax calls, I need to start a timer for each call, and clear the timer in both success and failure callbacks. Want to make sure that - if timer fires then it means request has not returned, so it should be aborted. For cases where inspite of the abort, the request may come back - want to discard them. Want to add a flag which checks success and failure callbacks before processing, and handles appropriately.

Want to add a timer like this :

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

But in the given existing code like below, I finding it hard on understanding where to start my timer and abort.

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

Any help is greatly appreciated.

There is no need to implement the logic yourself and the timeout option is provided by jquery, look at the documentation of jQuery.ajax() there is an option called timeout.

Ex:

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