在没有 AJAX 的情况下在 jQuery 中显示加载 GIF?
我知道如何做到这一点
$(gif).show();
$.post( action, data, function(result) { $(gif).hide(); } );
// using ajaxStart and ajaxStop is a better way too
但是,我不知道如何在不使用 post 的情况下做到这一点,我尝试过
$(gif).show();
doAction(); // No post...
$(gif).hide();
问题是,当 doAction 函数正在运行时,$(gif) 不可见.... I也尝试过超时,但问题是根据 doAction() 持续时间在之前或之后结束......
I know how to do this with
$(gif).show();
$.post( action, data, function(result) { $(gif).hide(); } );
// using ajaxStart and ajaxStop is a better way too
But, I don't know how to do it without using post, I tryed
$(gif).show();
doAction(); // No post...
$(gif).hide();
The problem is, while doAction function is running the $(gif) isn't visible .... I tryed with timeouts too, but the problem is that ends before or after depending of the doAction() duration...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
例如,当您的 doaction 执行某些 ajax 调用(搜索)时, $(gif).hide() 可能会触发,这需要时间。
将 hide 函数放在函数(结果)之后的目的是,只有当您的 ajax 调用返回结果时才会调用它。
如果您不想依赖 ajax Complete 来显示或隐藏加载程序,您可以将结果放在覆盖加载程序的 DIV 层上。 (较高的 z 指数)
$(gif).hide() can fire while your doaction is performing some ajax call (search) for example, which takes time.
The purpose of putting the hide function after the function(result) is so that it is only called when your ajax call comes back with the results.
If you do not want to depend on ajax complete to show or hide your loader, you can put your results on a DIV layer which covers the loader. (higher z-index)
如果 doAction() 所做的任何事情都是同步的,那应该可以工作。
如果不是,那么您将需要在操作(无论是什么)完成时触发的回调中调用 hide 方法。
That should work, if whatever
doAction()
does is synchronous.If it isn't, then you will need to call the hide method in a callback that fires when the operation (whatever it is) finishes.