1

在 jQuery Mobile 中,如果用户单击一个按钮,则会出现一个加载图标,然后它会通过 Ajax 加载新网页。

但是,在我的情况下,服务器可能没有响应。没有办法为ajax 导航功能设置超时(例如10 秒)吗?(如果超过时间限制,停止尝试导航并显示错误消息)

4

1 回答 1

1

要设置超时,我认为您不应该以静态方式(使用“数据转换”)进行设置,您可以创建链接的侦听器(“onclick”)并在侦听器中进行 ajax 调用以加载您的页面。用来$.mobile.changePage()做那个。

$.mobile.changePage()函数在 jQuery Mobile 中的许多地方使用。例如,当一个链接被点击时,它的href属性被规范化,然后$.mobile.changePage()处理其余的。

所以你的代码看起来像这样:

$('#link_id').click(function() {
    $.ajax({
    url: "page_served_from_server",
        error: function(jqXHR, strError){
        if(strError == 'timeout')
        {
          //do something. Try again perhaps?
        }
    },
    success: function(){
        //charge your page :
       // $.mobile.changePage('yourPageAdress',"turn",false,true);
    },
    // here you can specify your timeout in milliseconds 
    timeout:3000
    });
});
于 2012-08-05T16:35:23.550 回答