Kill the Javascript process after a certain time

I apologize if this is a duplicate.

Let's say I have a JavaScript function that calls a web service to pull some data. I use some kind of moving graphics so that the user knows that it works. When it is successfully restored, I change the graphics to a check mark. Here is my code:

getData: function() {
    $("#button").attr("disabled", "true");

    var params = {
        doRefresh: false,
        method: '/GetData',
        onSuccess: new this.getDataCallback(this).callback,
        onFailure: new this.getDataFailed(this).callback,
        args: { text: $("#getData").val() }
    };

        WebService.invoke(params.method, params.onSuccess, params.onFailure, params.args);

}

What I need is after 5 minutes, if this process still could not successfully return my data, throw an exception, or, even better, run my this.getDataFailed (this) .callback function. Does this look like JavaScript? I looked at setTimeout () and setInterval (), and they seem to just delay the execution of the script, while I literally want to “time out” a long process. Any ideas?

, / , .

+5
2

IE8 timeout ontimeout. .

, jQuery . jQuery timeout ajax(), error . $.ajaxSetup() ajax:

$.ajaxSetup({
    timeout: 300000
});

jQuery , -:

var xhrTimeout;
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://blah.com/etc", true);
xhr.onreadystatechange = function ()
{
    if (xhr.readyState == 4)
        window.clearTimeout(xhrTimeout);
}
xhr.send();
xhrTimeout = window.setTimeout(function ()
{
    xhr.abort();
    failFunction();
}, 300000); // 5 mins
+5

, , ( psuedocode):

InvokeWebService
setTimeout

onTimeout:
    cancelInvokation
    DoOtherStuff

onWebServiceCompletion:
    cancelOnTimeout
+3

All Articles