jQuery Mobile 手动显示ajax加载器

时间:2023-03-08 19:33:19

在jquery mobile开发中,经常需要调用ajax方法,异步获取数据,如果异步获取数据方法由于网速等等的原因,会有一个反应时间,如果能在点击按钮后数据处理期间,给一个正在加载的提示,客户体验会更好一些。

先看两个方法,显示和关闭,方法来自于参考:http://blog.****.net/zht666/article/details/8563025

jQuery Mobile 手动显示ajax加载器
<script>
//显示加载器
function showLoader() {
//显示加载器.for jQuery Mobile 1.2.0
$.mobile.loading('show', {
text: '加载中...', //加载器中显示的文字
textVisible: true, //是否显示文字
theme: 'a', //加载器主题样式a-e
textonly: false, //是否只显示文字
html: "" //要显示的html内容,如图片等
});
} //隐藏加载器.for jQuery Mobile 1.2.0
function hideLoader()
{
//隐藏加载器
$.mobile.loading('hide');
}
</script>
jQuery Mobile 手动显示ajax加载器

然后在ajax中调用:

jQuery Mobile 手动显示ajax加载器
//获取进度
function InsertStatus(matterID, obj) {
var a = $(obj).parent().parent().parent();
$.ajax({
type: "POST",
contentType: "application/json",
url: "/ToDoList/InsertStatus/?matterID=" + matterID,
beforeSend: function () {
showLoader();
},
complete:function(){
hideLoader();
},
success: function (msg) {
if (msg > 0) {
$("#popdiv").text("获取进度成功");
} else {
$("#popdiv").text("获取进度失败");
}
//弹出提示信息
$("#GettingProgress").attr('data-rel', 'dialog');
$("#GettingProgress").trigger('create');
$("#popdiv").popup("open");
setTimeout(function () { $("#popdiv").popup("close"); }, 2000);
}
}); }
jQuery Mobile 手动显示ajax加载器

这样就可以在数据处理过程中,有加载中的效果。

参考:http://www.cnblogs.com/hiflora/p/3816212.html

http://blog.****.net/zht666/article/details/8563025