Consider marking event handler as 'passive' to make the page more responsive

故事扮演 提交于 2019-11-26 12:16:44

问题


I am using hammer for dragging and it is getting choppy when loading other stuff, as this warning message is telling me.

Handling of \'touchstart\' input event was delayed for X ms due to main thread being busy. Consider marking event handler as \'passive\' to make the page more responsive.

So I tried to add \'passive\' to the listener like so

Hammer(element[0]).on(\"touchstart\", function(ev) {
  // stuff
}, {
  passive: true
});

but I\'m still getting this warning.


回答1:


For those receiving this warning for the first time, it is due to a bleeding edge feature called Passive Event Listeners that has been implemented in browsers fairly recently (summer 2016). From https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md:

Passive event listeners are a new feature in the DOM spec that enable developers to opt-in to better scroll performance by eliminating the need for scrolling to block on touch and wheel event listeners. Developers can annotate touch and wheel listeners with {passive: true} to indicate that they will never invoke preventDefault. This feature shipped in Chrome 51, Firefox 49 and landed in WebKit. For full official explanation read more here.

See also: What are passive event listeners?

You may have to wait for your .js library to implement support.

If you are handling events indirectly via a JavaScript library, you may be at the mercy of that particular library's support for the feature. As of August 2016, it does not look like any of the major libraries have implemented support. Some examples:

  • jQuery.js - ongoing issue: https://github.com/jquery/jquery/issues/2871
  • React.js - ongoing issue: https://github.com/facebook/react/issues/6436
  • Hammer.js - ongoing issue: https://github.com/hammerjs/hammer.js/pull/987
  • perfect-scrollbar - ongoing issue: https://github.com/noraesae/perfect-scrollbar/issues/560
  • AngularJS - ongoing issue: https://github.com/angular/angular.js/issues/15901



回答2:


this hide the message

    jQuery.event.special.touchstart = 
    {
      setup: function( _, ns, handle )
      {
        if ( ns.includes("noPreventDefault") ) 
        {
          this.addEventListener("touchstart", handle, { passive: false });
        } 
        else 
        {
          this.addEventListener("touchstart", handle, { passive: true });
        }
      }
    };



回答3:


I found a solution that works on jQuery 3.4.1 slim

After un-minifying, add {passive: true} to the addEventListener function on line 1567 like so:

t.addEventListener(p, a, {passive: true}))

Nothing breaks and lighthouse audits don't complain about the listeners.




回答4:


For those stuck with legacy issues, find the line throwing the error and add {passive: true} - eg:

this.element.addEventListener(t, e, !1)

becomes

this.element.addEventListener(t, e, { passive: true} )


来源:https://stackoverflow.com/questions/39152877/consider-marking-event-handler-as-passive-to-make-the-page-more-responsive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!