在 jQuery Mobile 中,如果用户单击按钮,则会出现一个加载图标,然后通过 Ajax 加载新网页。
但是,在我的情况下,服务器可能没有响应。有没有办法为ajax导航功能设置超时(例如10秒)? (如果超过时间限制,停止尝试导航并显示错误消息)
最佳答案
要设置超时,我认为您不应该以静态方式执行此操作(使用“数据转换”)
您可以为链接创建一个监听器(“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
});
});
关于javascript - jQuery Mobile Ajax 导航超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11816898/