设置ajax加载时间长,关于javascript:如何设置$ .ajax请求的超时并重做它,如果它需要太长时间?...

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

这是我的$ .ajax请求。

$.ajax({

url: '<?php bloginfo('template_directory'); ?>/ajax/product.php',

type: 'get',

data: {product_id : product_id},

beforeSend: function(){

$('#details').html('');

},

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请求?

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.

如果只有一种类型,即发出请求的URL,这将起作用。 尽管如此,整洁的答案!:)

工作就像一个魅力。 谢谢

对于这种情况,我会使用jquery延迟失败回调。

http://api.jquery.com/deferred.fail/

一个。 您可以维护所有请求的队列。

$.xhrQueue = [];

湾 将您提出的每个请求排入队列

$.ajaxSetup({

beforeSend: function (e, xhr) {

$.xhrQueue .push(xhr);

}

});

C。 完成请求与超时的轮询

setInterval(function(){

$.each($.xhrQueue, function (xhr) {

//check whether status is complete,

//with some try-catch you can know which were the timed-out/not-sent ones

});

}, 1000);

注意:如果不是beforeSend,您可以通过其他一些函数来尝试进行ajax调用 sub>

试试这个

var setTime = setTimeOut(function() {

$.ajax({

url: '<?php bloginfo('

template_directory ');?>/ajax/product.php',

type: 'GET',

data: {

'product_id': product_id

}

}).beforeSend(function() {

$('#details').html('');

}).done(function(data) {

$('.iosSlider').fadeOut('fast');

thisprod.addClass('current');

$('#details').css({

opacity: 0

}).html(data).stop().animate({

left: 0,

opacity: 1

}, 800);

});

}, 2000);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值