Javascript getJSON 超时处理

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

getJSON timeout handling

javascriptjqueryajaxjson

提问by pheaselegen

I am using jQuery getJSON()function. This function getting data with no problem. But sometimes waiting, waiting waiting... And my loading bar showing loading loading loadin at center of page. So jQuery ajax()function have an timeout variable. But i want to use getJSONfunction. And i think that i can use ajaxStart()and ajaxStop()functions. But how?

我正在使用 jQuerygetJSON()函数。此功能获取数据没有问题。但有时等待,等待等待......我的加载栏在页面中心显示正在加载加载加载。所以 jQueryajax()函数有一个超时变量。但我想使用getJSON功能。而且我认为我可以使用ajaxStart()ajaxStop()运行。但是如何?

$('.loadingDiv')
    .hide()
    .ajaxStart(function() {
        $(this).fadeIn();
        setTimeout("throw '';",15000) //i used this but didn't work
        setTimeout("return;",15000) //i used this but didn't work
        setTimeout("abort();",15000) //i used this but didn't work.(Abort all ajax events)
    })
    .ajaxStop(function() {
        $(this).fadeOut();
    });

采纳答案by Denys Séguret

getJSON()returns a promise on which you can call the abortfunction :

getJSON()返回一个你可以调用abort函数的承诺:

var p = $.getJSON(..., function(){ alert('success');});
setTimeout(function(){ p.abort(); }, 2000);

EDIT :but if your goal is just to abort if it takes too much time, then lethal-guitar's answer is better.

编辑:但是如果您的目标只是在花费太多时间时中止,那么致命吉他的答案会更好。

回答by lethal-guitar

getJSON()is just a shorthand for the following:

getJSON()只是以下内容的简写:

$.ajax({
    dataType: "json",
    url: url,
    data: data,
    success: success
});

So you could use $.ajax()and specify the timeoutoption as desired. See also: http://api.jquery.com/jQuery.getJSON/

因此,您可以根据需要使用$.ajax()和指定该timeout选项。另见:http: //api.jquery.com/jQuery.getJSON/

回答by Bruno

As lethal-guitar mentioned getJSON()function is just an shorthand for $.ajax(). If you want to detect if a timeout has occurred rather than an actual error use the code below.

正如 lethal-guitar 提到的那样,getJSON()函数只是$.ajax(). 如果您想检测是否发生超时而不是实际错误,请使用以下代码。

var request = $.ajax({
    dataType: "json",
    url: url,
    data: data,
    success: function( ) { },
    timeout: 2000
}).fail( function( xhr, status ) {
    if( status == "timeout" ) {
        // do stuff in case of timeout
    }
});

回答by Richard

There's always the nuclear route as well:

也总是有核路线:

//Set AJAX timeout to 10 seconds
$.ajaxSetup({
  timeout: 10*1000
});

This will set all the AJAX requests your program makes (even via $.getJSON) to have a time out of 10 seconds (or what have you).

这会将您的程序发出的所有 AJAX 请求(甚至通过 $.getJSON)设置为 10 秒(或您有什么)。

回答by renab

the setTimeout function executes a set of code after a specified number of milisecons in the global scope.

setTimeout 函数在全局范围内指定的毫秒数后执行一组代码。

The getJSON function (per the jQuery documentation here http://api.jquery.com/jQuery.getJSON/) is shorthand for:

getJSON 函数(根据此处的 jQuery 文档http://api.jquery.com/jQuery.getJSON/)是以下内容的简写:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

so you would want to make your call like so:

所以你想像这样拨打电话:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success,
  timeout: 15000
});

$('.loadingDiv')
    .hide()
    .ajaxStart(function() {
        $(this).fadeIn();
    })
    .ajaxStop(function() {
        $(this).fadeOut();
    });

回答by serraosays

I don't think any of these answers are ideal. I know this is years late, but what you want to do is use the success/error callback options of the .ajax();method when receiving a JSONP response.

我认为这些答案中的任何一个都不理想。我知道这已经晚了好几年了,但是您想要做的是.ajax();在接收 JSONP 响应时使用该方法的成功/错误回调选项。

Example of how I would structure this:

我将如何构建此示例的示例:

    // Call
    $.ajax({

      // URL you want to get
      url: 'http://example.com/json?callback=?',

      // Set a realistic time in milliseconds
      timeout: 3000,

      // Put in success callback function here, this example
      // shows you the data you got back from the call
      success: function(data) {
        console.log(data);
      },

      // Put in an error handling function, just an alert in this case
      error: function(badData) {
        alert('The call was unsuccessful');
      },

      type: 'POST'
    });