我有这个代码从
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); } } }); }