显示元件只有的getJSON时间超过n毫秒? [英] Show element only if getJSON takes more than n milliseconds?
本文介绍了显示元件只有的getJSON时间超过n毫秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些JavaScript:
I have some JavaScript:
surveyBusy.show();
$.getJSON(apiUrl + '/' + id)
.done(function (data) {
...
surveyBusy.hide();
})
.fail(function (jqXHR, textStatus, err) {
...
surveyBusy.hide();
});
不过,我想唯一的问题 surveyBusy.show();
如果 $的getJSON
需要更多比 N
的毫秒数。你得到一个闪烁,否则。是否有关于的getJSON
API,可以做到这一点的回调?我看到没有在文档。
However, I'd like to only issue surveyBusy.show();
if $.getJSON
takes more than n
number of milliseconds. You get a flicker otherwise. Is there a callback on the getJSON
api that can do this? I see nothing in the documentation.
推荐答案
只需使用超时。另外,我把你的隐藏code。在总是
处理程序,以减少code重复。
Just use a timeout. Also, I put your "hide" code in the always
handler to reduce code repetition.
var busyTimeout = setTimeout(function() { surveyBusy.show(); }, 2000);
$.getJSON(apiUrl + '/' + id)
.done(function (data) {
...
})
.fail(function (jqXHR, textStatus, err) {
...
})
.always(function() {
clearTimeout(busyTimeout);
surveyBusy.hide();
});
这篇关于显示元件只有的getJSON时间超过n毫秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文