等待 JQuery 完成 .live() 或 .bind() 绑定?

发布于 2024-09-13 17:58:41 字数 352 浏览 4 评论 0原文

我有一个脚本,通过 JQuery .live 或 .bind (尚未决定使用哪个)将单击事件处理程序添加到页面中的大约 500 个元素,并且我遇到了 JQuery 性能问题。

使用小数据集,我可以加载页面,一旦页面根据浏览器完成加载,我就可以单击某些内容然后开始。但是增加数据集,页面加载结束和我的点击被处理之间存在一定的延迟,我只能假设 JQuery 仍在附加事件处理程序 - 点击被正确处理,所以我假设它已排队然后处理。

有没有办法让 JQuery 在继续之前等待 .live() 完成,或者换句话说,如果有意义的话,同步处理它而不是异步处理它?这样我就可以在开始 .live() 调用之前放置一个加载屏幕,并在完成后将其取下。

问候 哞

I have a script which adds click event handlers to about 500 elements in the page via JQuery .live or .bind (not decided which yet), and I am having a JQuery performance issue.

With a small dataset, I can load the page and as soon as the page has finished loading according to the browser, I can click on something and off we go. But increase the data set and there is a definite delay between the page load ending and my click being handled, which I can only assume is JQuery still working off attaching the event handlers - the click is handled correctly, so I am assuming it gets queued and then handled.

Is there any way to have JQuery wait for the .live() to finish before moving on, or in other words handle it synchronously rather than asynchronously if that makes sense? This is so I can put a loading screen up before starting the .live() call, and take it down when its done.

Regards
Moo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

爱她像谁 2024-09-20 17:58:41

jQuery .live() 处理程序(或 .delegate()) 应该非常很快完成,不像 < a href="http://api.jquery.com/bind/" rel="nofollow noreferrer">.bind() 绑定每个元素。在这些情况下,您应该使用 .live().delegate(),因为.bind() 远远超过父元素上处理程序的事件冒泡/监听成本。

A jQuery .live() handler (or .delegate()) should finish very quickly, unlike .bind() which binds each element. In these situations you should use .live() or .delegate(), since the startup cost of .bind() far exceeds the event bubbling/listening cost of a handler on a parent element.

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