var refreshId_hxlatestposts = setInterval(function() {
var el = $("#hxlatestposts");
var req = $.get("example.php");
el.fadeOut('slow', function () {
req.done(function( data ){
el.html(data).fadeIn('slow');
});
});
}, 60000);
这是我用来每分钟刷新一个 div 的东西,有时它会在它从中获取提要的站点关闭或其他原因时挂起。我想知道如何设置超时,这样如果它无法在 X 秒内加载 php 文件,则返回“加载失败”。
最佳答案
jQuery 文档 ( .ajaxSetup()) 建议使用 .ajaxSetup()
来设置 timeout
的值,而不是在单独的请求中使用它。
如果请求失败,您可以使用 request.fail()
注册一个函数。
$.ajaxSetup({
timeout: 5000
});
var refreshId_hxlatestposts = setInterval(function() {
var el = $("#hxlatestposts");
var req = $.get("example.php");
el.fadeOut('slow', function() {
req.done(function(data) {
el.html(data).fadeIn('slow');
});
req.fail(function(jqXHR, textStatus) {
el.html('Fail to load').fadeIn('slow');
});
});
}, 60000);
关于javascript - 为 load() 添加超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11584891/