如何设置超时的$就要求重做它,如果它的时间太长? [英] How to set timeout on $.ajax request and redo it if it takes too long?
问题描述
有人能告诉我一个超时设置为我的$就要求一个实际的例子和重做整个请求,如果第一个请求超时,我读过的文档,并没有得到它。我将AP preciate任何帮助。
下面是我的$阿贾克斯的要求。
$。阿贾克斯({
网址:'?< PHP代码bloginfo('template_directory'); ?> /ajax/product.php',
输入:'得到',
数据:{PRODUCT_ID:PRODUCT_ID},
beforeSend:函数(){
$('#细节)HTML('< DIV CLASS =加载>< / DIV>');
},
成功:功能(数据){
$('。iosSlider)淡出(快)。
thisprod.addClass(当前);
。$('#详细信息)的CSS({不透明度:0})HTML(数据).stop()动画({左:0,不透明度:1},800);
}
});
返回false;
在阿贾克斯功能需要一个超时参数,你可以检查在发生故障时的状态。
VAR调用=函数(){
$阿贾克斯({
网址:'?< PHP代码bloginfo('template_directory'); ?> /ajax/product.php',
输入:'得到',
超时:400,
...
错误:函数(X,textStatus,M){
如果(textStatus ==超时){
呼叫();
}
}
});
};
您可能希望使一些聪明一点,以避免永久性的呼叫...
从文档:
设置超时时间(毫秒)的请求。这将覆盖 任何全局超时设置$ .ajaxSetup()。超时周期开始 在点$就调用时;如果其他几个请求在 进度和浏览器没有可用的连接,就可以 在请求超时之前,它可以被发送。在jQuery的1.4.x的和 下面,对XMLHtt prequest对象将是处于无效状态,如果 请求超时;访问任何对象成员可能会引发 例外。在Firefox 3.0+只,脚本和JSONP请求不能 由超时被取消;如果以后到达的脚本将仍然运行 超时时间。
Can someone show me a practical example on setting a timeout to my $.ajax request and redo the entire request if the first request is timed out, I've read the docs and didn't get it. I will appreciate any help.
Here is my $.ajax request.
$.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;
The ajax function takes a timeout parameter and you can check the status in case of error.
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();
}
}
});
};
You might want to make something a little smarter to avoid permanent calls...
From the documentation :
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.
这篇关于如何设置超时的$就要求重做它,如果它的时间太长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!