timeout - Kill a Javascript process after a set amount of time - Stack Overflow

I apologize if this is a duplicate.Let's say I have a JavaScript function that calls a web service

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 graphic to let the user know it's working. When it is successfully retrieved, I change the graphic to a check mark. Here's 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'd like is after 5 minutes, if this process still has not successfully returned my data, to throw an exception or, better yet, run my function this.getDataFailed(this).callback. Does this seem feasible with JavaScript? I've looked at setTimeout() and setInterval(), and these appear to just delay the execution of a script, whereas I want to literally "timeout" a long running process. Any ideas?

Also, I'm open to any criticism / improvements to my code that would allow for this functionality.

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 graphic to let the user know it's working. When it is successfully retrieved, I change the graphic to a check mark. Here's 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'd like is after 5 minutes, if this process still has not successfully returned my data, to throw an exception or, better yet, run my function this.getDataFailed(this).callback. Does this seem feasible with JavaScript? I've looked at setTimeout() and setInterval(), and these appear to just delay the execution of a script, whereas I want to literally "timeout" a long running process. Any ideas?

Also, I'm open to any criticism / improvements to my code that would allow for this functionality.

Share Improve this question edited Feb 19, 2010 at 0:50 karlgrz asked Feb 19, 2010 at 0:43 karlgrzkarlgrz 14.8k12 gold badges48 silver badges58 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

IE8 offers a timeout property and ontimeout event. These are non-standard however.

I noticed you're using jQuery in some of your example code. jQuery also supports a timeout property for its ajax() method which will execute the error callback after the timeout limit has been reached. You can use the $.ajaxSetup() method to declare the timeout before making any ajax calls:

$.ajaxSetup({
    timeout: 300000
});

If you're not using jQuery to make your requests you can roll your own timeout code:

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

The way I would do it would look like this (very rough psuedocode):

InvokeWebService
setTimeout

onTimeout:
    cancelInvokation
    DoOtherStuff

onWebServiceCompletion:
    cancelOnTimeout

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745079106a4610011.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信