gpt4 book ai didi

javascript - 如何在 $.ajax 请求上设置超时并在超时时重做?

转载 作者:行者123 更新时间:2023-11-29 17:21:36 25 4
gpt4 key购买 nike

谁能告诉我一个关于为我的 $.ajax 请求设置超时并在第一个请求超时时重做整个请求的实际示例,我已经阅读了文档但没有得到它。我将不胜感激任何帮助。

这是我的 $.ajax 请求。

    $.ajax({
url: '<?php bloginfo('template_directory'); ?>/ajax/product.php',
type: 'get',
data: {product_id : product_id},
beforeSend: function(){
$('#details').html('<div class="loading"></div>');
},
success: function(data){
$('.iosSlider').fadeOut('fast');
thisprod.addClass('current');
$('#details').css({opacity: 0}).html(data).stop().animate({left: 0, opacity: 1}, 800);
}
});
return false;

最佳答案

ajax函数采用超时参数,您可以在出现错误时检查状态。

var call =function(){
$.ajax({
url: '<?php bloginfo('template_directory'); ?>/ajax/product.php',
type: 'get',
timeout: 400,
...
error: function(x, textStatus, m) {
if (textStatus=="timeout") {
call();
}
}
});
};

你可能想做一些更聪明的事情来避免永久调用......

来自文档:

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.

关于javascript - 如何在 $.ajax 请求上设置超时并在超时时重做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12266218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com