jQuery全局事件和性能? [英] jQuery global events and performance?

查看:129
本文介绍了jQuery全局事件和性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用jQuery显示状态指示器的方法,并且我在jQuery Cookbook中找到了一个解决方案,它显示了该解决方案

I was in search of a way to show a status indicator using jQuery and I found a solution in the jQuery Cookbook, it showed this solution

(function($) {
    $(document).ready(function() {
       $('#ajaxStatus')
         .ajaxStart(function() {
            $(this).show();
        })
         .ajaxStop(function() {
            $(this).hide();
        });

        .....
  })(jQuery);

然后它告诫

如果您在应用程序中遇到性能问题,可能是因为 事件传播的成本(如果存在很大的 元素数量.在这种情况下,将global设置为false可能会给您 性能上的改善.

If you experience performance issues in your application, it may be because of the cost of event propagation if there is a significantly large number of elements. In this case, setting global to false may give you a performance improvement.

所以首先我应该使用上面显示的解决方案,并且随着我的网站和js代码越来越大,这是否会成为性能问题?

So first should I use the solution showed above and will it be a performance issue as my site and js code gets larger and larger?

我应该避免jQuery全局事件,只是像段落中所述将其关闭吗?

Should I avoid jQuery global events, just turn it off like the paragraph says?

最后,你们如何在执行ajax请求时显示一个简单的指示器?

Lastly, how do you guys show a simple indicator while an ajax request is being performed?

推荐答案

老实说,我认为这种警告是有效的,但同时已经过时了.在此处查看jQuery源: http://github.com/jquery/jquery/blob/master/src/event.js#L290

Honestly, I think this caution is valid, but at the same time, outdated. Look at the jQuery source here: http://github.com/jquery/jquery/blob/master/src/event.js#L290

它显示了如何现在处理全局事件:

It shows how a global event is now handled:

if ( jQuery.event.global[ type ] ) {
  jQuery.each( jQuery.cache, function() {
    if ( this.events && this.events[type] ) {
      jQuery.event.trigger( event, data, this.handle.elem );
    }
  });
}

使用是:

jQuery("*").add([window, document]).trigger(type, data);

因此,它不会像以前那样在每个元素上触发,它使大量元素发出警告,这是一个非常现实的问题,而是循环遍历已注册或事件的元素,并且仅当任何已注册,因此现在的性能 更好.

So it's not triggering on every element like it used to, which made the large number of elements warning a very real concern, it instead loops over elements that have registered or the event, and only if any have registered, so it's much better on performance now.

对于您的问题:

那么我首先应该使用上面显示的解决方案,随着我的网站和js代码越来越大,这会成为性能问题吗?

So first should I use the solution showed above and will it be a performance issue as my site and js code gets larger and larger?

是的,上面的解决方案很好,在上面概述了全局事件优化之后,我还没有看到足够大的页面来解决这个问题.

Yes, the solution above is just fine, I've yet to see a page large enough for this to be an issue, after the global events optimization I outlined above.

我应该避免jQuery全局事件,只是像段落中所述将其关闭吗?

Should I avoid jQuery global events, just turn it off like the paragraph says?

不是,您几乎可以忽略该段落在此更改之后使用的任何jQuery 制成.

Nope, you can pretty much ignore that paragraph is using any jQuery after this change was made.

最后,你们如何在执行ajax请求时显示一个简单的指示器?

Lastly, how do you guys show a simple indicator while an ajax request is being performed?

与您的问题完全相同:)

Exactly as you have in your question :)

这篇关于jQuery全局事件和性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆