简体   繁体   English

jQuery全局事件和性能?

[英]jQuery global events and performance?

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 我正在寻找一种使用jQuery显示状态指示器的方法,我在jQuery Cookbook中找到了一个解决方案,它显示了这个解决方案

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

        .....
  })(jQuery);

then it cautioned, 然后它警告说,

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. 在这种情况下,将global设置为false可能会提高性能。

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? 首先,我应该使用上面显示的解决方案,这将是一个性能问题,因为我的网站和js代码变得越来越大?

Should I avoid jQuery global events, just turn it off like the paragraph says? 我应该避免jQuery全局事件,只需将其关闭就像段落所说的那样?

Lastly, how do you guys show a simple indicator while an ajax request is being performed? 最后,在执行ajax请求时,你们如何显示一个简单的指标?

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 在这里查看jQuery源代码: 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 );
    }
  });
}

It used to be: 曾经是:

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. 因此,这不是触发每个元素在像以前那样,这使得大量的警告一个非常现实的关注要素,它不是遍历已注册的元素或事件,且仅当任何已登记的,所以它的性能好得多现在。


For your questions: 对于你的问题:

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? 首先,我应该使用上面显示的解决方案,这将是一个性能问题,因为我的网站和js代码变得越来越大?

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. 是的,上面的解决方案很好,在上面概述的全局事件优化之后,我还没有看到足够大的页面成为一个问题。

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. 不,你可以在这个改变之后忽略该段落正在使用任何jQuery。

Lastly, how do you guys show a simple indicator while an ajax request is being performed? 最后,在执行ajax请求时,你们如何显示一个简单的指标?

Exactly as you have in your question :) 正如你在你的问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM