jQuery.get() 超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8535431/
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-27 10:39:08  来源:igfitidea点击:

Time-out with jQuery.get()

jqueryhtmlajax

提问by Sander

Is there a way to set the timeout period in $.get()?

有没有办法设置超时时间$.get()

As I want my script to give a warning that loading is taking some time (perhaps due to a busy server or whatever) Can't seem to find it anywhere..

因为我希望我的脚本警告加载需要一些时间(可能是由于服务器繁忙或其他原因)似乎无法在任何地方找到它..

回答by Naftali aka Neal

Just use $.ajax(...):

只需使用$.ajax(...)

$.ajax({
   url: url,
   success: function(data){
        //...
   },
   timeout: 1000 //in milliseconds
});

Also as stated below in the comments you can use .ajaxSetup(...)to apply the timeout globally:

同样如下面的评论所述,您可以使用它.ajaxSetup(...)来全局应用超时:

$.ajaxSetup({timeout:1000}); //in milliseconds

回答by zzzzBov

$.getis just a nice shorthand for $.ajaxwhich has a timeoutoption, which will specify how long the script will wait before cancelling the callback.

$.get只是一个很好的简写,$.ajax它有一个timeout选项,它将指定脚本在取消回调之前等待多长时间。

You can override the global default using $.ajaxSetup({timeout: 9001}), which will allow you to continue to use $.get.

您可以使用 覆盖全局默认值$.ajaxSetup({timeout: 9001}),这将允许您继续使用$.get

If you simply want to alert the user without cancelling the request, use $.ajax, set a timeout, and cancel the timeout in the completecallback.

如果您只是想在不取消请求的情况下提醒用户,请使用$.ajax,设置超时,并在complete回调中取消超时。

Here's some example starter code, i haven't tested any of it, and it could probably be turned into a plugin with a bit of work:

这是一些示例启动代码,我还没有测试过它,它可能会变成一个需要一些工作的插件:

(function(){
  var t, delay;
  delay = 1000;
  $('.delay-message').ajaxStart(function () {
  var $this;
  $this = $(this);
  t = setTimeout(function () {
    $this.trigger('slowajax');
  }, delay);
  }).ajaxComplete(function () {
    if (t) {
      clearTimeout(t);
    }
  });
}());

$('.delay-message').on('slowajax', function () {
  $(this).show().delay(2000).fadeOut(1000);
});

回答by Rafay

use ajaxSetup

ajaxSetup

$.ajaxSetup({
timeout:2000 // in milliseconds 
});

//your get request here

回答by SLaks

Call $.ajaxand pass the timeoutoption.

调用$.ajax并传递timeout选项。

回答by jabclab

Just use .ajax()for this; after all .get()is just a wrapper around this. You can do something like:

仅用.ajax()于此;毕竟.get()只是一个包装。您可以执行以下操作:

function doAjax() {
    var $warning = $("#warning").hide(), // Always hide to start with
        warningTimeoutId = 0;

    warningTimeoutId = window.setTimeout(function () {
         $warning.show();
    }, 10000); // wait 10s before showing warning

    $.ajax({
        timeout: 30000, // 30s timeout
        complete: function () {
            // Prevent the warning message from appearing
            window.clearTimeout(warningTimeoutId);
        }
        ... // other config
    });
}

Here you start a timeout just before the Ajax call which will be shown after 10s unless the Ajax request has already finished.

在这里,您在 Ajax 调用之前开始超时,除非 Ajax 请求已经完成,否则将在 10 秒后显示。

回答by Dion

If you want to easily display a custom error messagewhen a timeout occurs (otherwise the $.get-completion callback is not called) you can use this:

如果您想在超时发生时轻松显示自定义错误消息(否则不会调用 $.get-completion 回调),您可以使用以下命令:

$.ajaxSetup({
    timeout: 1000,
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        if (textStatus == 'timeout') {
            // timeout occured
        } else {
            // other error occured (see errorThrown variable)
        }
    }
});