Javascript 如何使用jQuery设置ajax帖子的最大执行时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10354685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:00:37  来源:igfitidea点击:

How to set maximum execution time for ajax post with jQuery?

javascriptajaxjquery

提问by themhz

Is there a way to specify maximum execution time of an ajax post to the server so if the server doesn't respond, then keep trying for 10 seconds and then continue with the rest of the code??

有没有办法指定 ajax post 到服务器的最大执行时间,所以如果服务器没有响应,那么继续尝试 10 秒,然后继续执行其余的代码?

Function doajaxPost(){
    var returned_value="";

    // #############I NEED THIS CODE TO TRY TO POST THE DATA TO THE SERVER AND KEEP
    // #############TRYING FOR 10 SECONDS AND THEN CONTINUE WITH THE REST OF THE CODE.
        jQuery.ajax({
            url: 'ajaxhandler.php',
            success: function (result) {                               
                returned_value=result;
            },
            async: false
        });
    // ###################################################

    alert(returned_value);

    some other code
    .
    .
    .           
 }

回答by ilyes kooli

Use timeout:

使用timeout

jQuery.ajax({
    url: 'ajaxhandler.php',
    success: function (result) {                               
        returned_value=result;
    },
    timeout: 10000,
    async: false
});

However, alert(returned_value);will execute just after your call (won't wait for the call to finish).

但是,alert(returned_value);将在您调用后立即执行(不会等待调用完成)。

回答by John Fisher

The JQuery API documentation tells how to set a "timeout".

JQuery API 文档介绍了如何设置“超时”。

http://api.jquery.com/jQuery.ajax/

http://api.jquery.com/jQuery.ajax/

While other answers here are correct, learning to check the documentation for yourself is more valuable than knowing just this answer.

虽然这里的其他答案是正确的,但学习自己检查文档比只知道这个答案更有价值。

回答by BluesRockAddict

You can set timeout value for your ajax request.

您可以为 ajax 请求设置超时值。

timeout

暂停

Set a timeout (in milliseconds) for the request. This will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent. In jQuery 1.4.x and below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period.

为请求设置超时(以毫秒为单位)。这将覆盖使用 $.ajaxSetup() 设置的任何全局超时。超时时间从 $.ajax 调用开始;如果其他几个请求正在进行中并且浏览器没有可用的连接,则请求可能会在发送之前超时。在 jQuery 1.4.x 及以下版本中,如果请求超时,XMLHttpRequest 对象将处于无效状态;访问任何对象成员都可能引发异常。仅在 Firefox 3.0+ 中,脚本和 JSONP 请求不能被超时取消;即使脚本在超时后到达,脚本也会运行。

Here is an example:

下面是一个例子:

$.ajax({
   url: "ajaxhandler.php",
   ...
   timeout: 10000,
   ... 
});

回答by gayu

Set a timeout (in milliseconds) for the request. This will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent. In jQuery 1.4.x and below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period.

为请求设置超时(以毫秒为单位)。这将覆盖使用 $.ajaxSetup() 设置的任何全局超时。超时时间从 $.ajax 调用开始;如果其他几个请求正在进行中并且浏览器没有可用的连接,则请求可能在发送之前超时。在 jQuery 1.4.x 及以下版本中,如果请求超时,XMLHttpRequest 对象将处于无效状态;访问任何对象成员都可能引发异常。仅在 Firefox 3.0+ 中,脚本和 JSONP 请求不能被超时取消;即使脚本在超时后到达,脚本也会运行。