在没有 AJAX 的情况下在 jQuery 中显示加载 GIF?

发布于 2024-09-25 05:07:45 字数 408 浏览 1 评论 0原文

我知道如何做到这一点

$(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除会话 2024-10-02 05:07:46

例如,当您的 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)

も星光 2024-10-02 05:07:45

如果 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文