大多数情况下,获得响应所需的时间少于秒,因此我可以向用户显示结果.
但有时(大约5%的重新加载)需要几秒钟(我很好,服务器有点忙).
我想在超过2秒的时间内显示“请稍候”文字.但是当它花费不到2秒时(因此用户不会因快速显示/隐藏消息而感到困惑).
如何以最有效和最简单的方式制作它?
当前的jQuery代码:
jQuery.ajax({
url: "loadData.PHP",
dataType: 'json',
type: 'GET',
success: function(result){
showResultsToUser(result);
}
});
解决方法:
像这样的东西:
var cancelMe = setTimeout(function() {
$('#loading').show(); // assuming your loading element has the id "loading"
}, 2000); // show loading element in 2 seconds
jQuery.ajax({
url: "loadData.PHP",
dataType: 'json',
type: 'GET',
success: function(result){
showResultsToUser(result);
clearTimeout(cancelMe); // don't run if it hasn't run yet
$('#loading').hide(); // hide the loading element if it's shown
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。