加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax – 是否可以检查jQuery.post()的超时?

发布时间:2020-12-16 01:35:11 所属栏目:百科 来源:网络整理
导读:我有这个代码从 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",{ up
我有这个代码从 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()上设置超时检查?例如:如果$.post()使用多于X ms,请重新加载请求.

更新:看起来我发现了一个解决方案:

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,dataType: "json",success:function(data) {
            if (data == null){
            alert('ajax failed. reloading...');
            gotoDir(pmcat_id,pcat_id);
        } else {
            $('#pageSlideshow').html(data.content);
        }
        }        
    });
}

这样做可行吗? :S

$.ajax具有完成您要求的所有功能:
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",timeout: 500,// in milliseconds
        success: function(data) {
            // process data here
        },error: function(request,status,err) {
            if(status == "timeout") {
                gotoDir(pmcat_id,pcat_id);
            }
        }
    });
}

请注意,您不需要设置超时选项,除非您希望在要设置的特定时间后触发错误方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读