如何在昂贵的 jquery 操作期间显示进度图标?
我有以下代码来显示进度 ajax 图标,然后运行一堆 javascript,然后隐藏进度图标。
运行此命令时,我根本看不到 div#collapseExpandProgress
变得可见。
我的猜测是:
- 它根本不显示
- 它立即显示并隐藏,因为
each()
方法在回调方法之外工作
$("#expandAll").click(function (e) {
$('#collapseExpandProgress').show();
$(".treeclick", "#treegrid").each(function () {
if ($(this).hasClass("tree-plus")) {
$(this).trigger("click");
}
});
$('#collapseExpandProgress').hide();
});
对于这里可能出现的问题有什么建议吗?
I have the following code to show a progress ajax icon and then run a bunch of javascript and then hide the progress icon.
When running this I don't see the div#collapseExpandProgress
become visible at all.
My guess is either:
- It's not showing at all
- It's showing and hiding immediately because the
each()
method works off the callback method
$("#expandAll").click(function (e) {
$('#collapseExpandProgress').show();
$(".treeclick", "#treegrid").each(function () {
if ($(this).hasClass("tree-plus")) {
$(this).trigger("click");
}
});
$('#collapseExpandProgress').hide();
});
Any suggestion on what could be wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有完整的技术解释,但我以前经历过这种情况。这可能与 javascript 控制流的工作方式有关。大概是因为 Javascript 是单线程的,所以直到操作的同步部分完成之后,DOM 才会更新。
我还发现,在类似情况下,gif 动画可能会显示但不会动画。
您很可能可以像这样显示 gif:
但是,我怀疑它不会动画化,原因与它最初没有显示的原因相同,可能取决于浏览器。
I don't have a complete technical explanation, but I have experienced this before. It probably has something to do with the way javascript control flow works. Presumably because Javascript is single threaded, the DOM is not updated until after the synchronous part of your operation has completed.
I've also found that an animated gif may show but not animate in similar circumstances.
You can most likely get the gif to show like this:
However, I suspect it will not animate for the same reason it didn't show in the first place, perhaps depending on the browser.
在您要进行的
each()
的最后一个回调中进行一次小的洗牌以触发hide()
应该可以解决问题。A minor shuffle to trigger the
hide()
on the last callback for theeach()
you've got going should do the trick.