是否有可能来检查超时的jQuery.post()? [英] Is it possible to check timeout on jQuery.post()?
问题描述
我有这个code,要求从MySQL数据库的一些文件夹信息:
I have this code requesting some folder info from a mysql DB:
function gotoDir(pmcat_id, pcat_id){
$('#slideshowContainer').html('<img class="loader" src="/javascript/ajax-loader.gif">');
$.post("/publish/includes/content.includes/functions.slideshow.php",
{ updateSlideshowDir: 1, pmcat_id: pmcat_id, pcat_id: pcat_id },
function(data){
$('#pageSlideshow').html(data.content);
}, "json"
);
}
有时候POST请求超时,因为不良的互联网连接。是否有可能设置$。员额超时检查()?例:如果$。员额()使用更多的则X毫秒,重载的要求
Sometimes the post request times out because of bad internet connection. Is it possible to set a timeout check on $.post() ? Ex: if $.post() uses more then X ms, reload the request.
更新:好像我相貌找到了解决方法:
UPDATE: Looks like I found a solution:
function gotoDir(pmcat_id, pcat_id){
$('#slideshowContainer').html('<img class="loader" src="/javascript/ajax-loader.gif">');
$.ajax({
type:"post",
url:"/publish/includes/content.includes/functions.slideshow.php",
data: { updateSlideshowDir: 1, pmcat_id: pmcat_id, pcat_id: pcat_id },
dataType: "json",
success:function(data) {
if (data == null){
alert('ajax failed. reloading...');
gotoDir(pmcat_id, pcat_id);
} else {
$('#pageSlideshow').html(data.content);
}
}
});
}
这是一个好办法做到这一点? :•
Is this a OK way to do this? :S
推荐答案
$阿贾克斯你需要完成你所要求的功能:
$.ajax has all the functions you need to accomplish what you are asking for:
function gotoDir(pmcat_id, pcat_id) {
$('#slideshowContainer').html('<img class="loader" src="/javascript/ajax-loader.gif">');
$.ajax({
type: "POST",
url: "/publish/includes/content.includes/functions.slideshow.php",
data: { updateSlideshowDir: 1, pmcat_id: pmcat_id, pcat_id: pcat_id },
dataType: "json",
timeout: 500, // in milliseconds
success: function(data) {
// process data here
},
error: function(request, status, err) {
if(status == "timeout") {
gotoDir(pmcat_id, pcat_id);
}
}
});
}
请注意,您并不需要设置的超时的选择,除非你希望以后在特定时间的您的要设定触发错误的方法。
Please note that you don't need to set the timeout option unless you want to trigger the error method after a specific time you want to set.
这篇关于是否有可能来检查超时的jQuery.post()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!