我试图在函数调用期间显示加载动画需要一些时间。函数调用正在搜索已加载的大型数组。搜索后,匹配项将插入表中。在开始搜索之前清除该表。
问题是动画仅在页面更新的短暂时刻显示。
这是我的代码:
var interval = setInterval(function ()
{
$.mobile.loading('show');
clearInterval(interval);
}, 1);
DoSearch(term, function ()
{
var interval = setInterval(function ()
{
$.mobile.loading('hide');
clearInterval(interval);
}, 1000);
});
//The search function looks like this (detail omitted for brevity):
function DoSearch(term)
{
$("table#tableICD tbody").html('');
// also tried:
/*$("table#tableICD tbody")
.html('')
.table()
.closest("table#tableICD")
.table("refresh")
.trigger("create");*/
var tr = '';
$.each(codes, function (key, value)
{
// determine which items match and add them as table rows to 'tr'
});
$("table#tableICD tbody")
.append(tr)
.closest("table#tableICD")
.table("refresh")
.trigger("create");
callback();
}
搜索正常工作并将行添加到表中。我有两个意想不到的行为:
到目前为止我读过的建议是使用setInterval而不是直接调用$ .mobile.loading,我已经完成了这项工作,并将搜索放在一个函数中并使用回调,我也做过。
有任何想法吗?
我来几点建议;它们可能无法解决您的所有问题,但它们可能会帮助您找到解决方案。
$(document).on('pagebeforecreate', '[data-role="page"]', function(){
setTimeout(function(){
$.mobile.loading('show');
},1);
});
$(document).on('pageshow', '[data-role="page"]', function(){
// You do not need timeout for pageshow. I'm using it so you can see loader is actualy working
setTimeout(function(){
$.mobile.loading('hide');
},300);
});