我正在使用 jQuery getJSON()
函数。这个函数获取数据没有问题。但有时等待,等待等待......我的加载栏在页面中心显示加载加载。
所以 jQuery ajax()
函数有一个超时变量。但我想使用 getJSON 函数。我认为我可以使用 ajaxStart()
和 ajaxStop()
函数。但如何呢?
$('.loadingDiv')
.hide()
.ajaxStart(function() {
$(this).fadeIn();
setTimeout("throw '';",15000) //i used this but didn't work
setTimeout("return;",15000) //i used this but didn't work
setTimeout("abort();",15000) //i used this but didn't work.(Abort all ajax events)
})
.ajaxStop(function() {
$(this).fadeOut();
});
最佳答案
getJSON()
返回一个 Promise,您可以在该 Promise 上调用 abort
函数:
var p = $.getJSON(..., function(){ alert('success');});
setTimeout(function(){ p.abort(); }, 2000);
编辑:但如果你的目标只是在花费太多时间的情况下中止,那么 lethal-guitar 的答案会更好。
关于javascript - getJSON 超时处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14238619/