为什么这个JavaScript代码会忽略超时?

时间:2011-07-08 07:30:34

标签: javascript

问题:

考虑下面的JavaScript代码,到目前为止工作正常,除了...
......它完全忽略了超时。

为什么?

我在ashx处理程序中使用睡眠10秒进行测试(超时设置为5秒),到目前为止它从未抱怨超时。当然,我会增加生产使用的超时时间。

function createXMLHttpRequest() 
{
    try { return new XMLHttpRequest(); } catch(e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
    alert("XMLHttpRequest not supported");
    return null;
}


function ExecuteSQL(strParameter1, strParameter2, strParameter3) 
{
    try 
    {
        strParameter1 = encodeURIComponent(strParameter1);
        strParameter2 = encodeURIComponent(strParameter2);
        strParameter3 = encodeURIComponent(strParameter3);

        var xhReq = createXMLHttpRequest();

        var dtNow = new Date();
        var dt = Date.parse(dtNow) + dtNow.getMilliseconds()

        var params = "prm1=" + strParameter1 + "&prm2=" + strParameter2 + "&prm3=" + strParameter3 + "&prm4=END";


        params = params + "&NoCache=" + dt;

        //var URLget = "cgi-bin/RequestData.ashx?NoCache=" + dt + "&param1=value1&param2=value2";
        var URLpost = "cgi-bin/RequestData.ashx?NoCache=" + dt;
        xhReq.open("POST", URLpost, false);

        //Send the proper header information along with the request
        xhReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhReq.setRequestHeader("Content-length", params.length);
        xhReq.setRequestHeader("Connection", "close");
        /*
        xhReq.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 00:00:00 GMT");
        */
        var MAXIMUM_WAITING_TIME = 5000;

        var requestTimer = setTimeout(function () {
            xhReq.abort();
            alert("Request cancelled. \nReason: Timeout (" + MAXIMUM_WAITING_TIME/1000 + "s) exceeded.");

            // Handle timeout situation, e.g. Retry or inform user.
        }, MAXIMUM_WAITING_TIME);
        xhReq.send(params);

        if (xhReq.status != 200) {
            clearTimeout(requestTimer);

            if (xhReq.status == 400) 
            {
                var serverResponse = xhReq.responseText;
                alert(unescape(serverResponse));
            }
            else
                alert("XMLHttpRequest Error\nHTTP Status: " + xhReq.status + "\nStatusText: " + xhReq.statusText);
        }
        else {
            clearTimeout(requestTimer);
            // Handle error, e.g. Dis
            var serverResponse = xhReq.responseText;
            alert("Successfully return from execution.");
            //alert(unescape(serverResponse));
        }

    } catch (ex) {
        alert("Error in JavaScript-function \"ExecuteSQL\".\nError description: " + ex.Description);
    }
} // End Sub ExecuteSQL

3 个答案:

答案 0 :(得分:3)

我要删除与计时器无关的所有内容,看看你的问题是否非常明显

function something() 
{
        var requestTimer = setTimeout(function () {
            alert("something");
        }, 1);

        if (something) {
            clearTimeout(requestTimer);
        }
        else {
            clearTimeout(requestTimer);
        } 
}

答案 1 :(得分:0)

函数Exec​​uteSQL

之外定义此变量 var requestTimer

答案 2 :(得分:0)

您正在使用XMLHTTPRequest进行同步调用。尝试通过在以下调用中将false更改为true来使其异步:

xhReq.open("POST", URLpost, true);

目前,由于您的同步通话,超时会被取消。