I am getting a weird warning while opening the application in chrome.I don't know how to get rid of this warning
[Violation] Added non-passive event listener to a scroll-blocking 'mousewheel' event. Consider marking event handler as 'passive' to make the page more responsive.
any one pls help me put on this.Thanks in advance
To fix this audit, add a passive: true flag to every event listener flagged by GTmetrix. For example, document. addEventListener('touchstart', onTouchStart, {passive: true});
Passive event listeners are an emerging web standard, new feature shipped in Chrome 51 that provide a major potential boost to scroll performance. Chrome Release Notes. It enables developers to opt-in to better scroll performance by eliminating the need for scrolling to block on touch and wheel event listeners.
you should use the passive option in . addEventListener(), which doesn't block the event while your listener executes. document. addEventListener("mousewheel", somefunction(), { passive: false });
Touch and wheel event listeners are useful for tracking user interactions and creating custom scrolling experiences, but they can also delay page scrolling. Currently, browsers can't know if an event listener will prevent scrolling, so they always wait for the listener to finish executing before scrolling the page.
There is an update of the API of event listeners.
In short this:
document.addEventListener('touchstart', handler, true);
becomes this:
document.addEventListener('touchstart', handler, {capture: true});
Since in your case you attach event listener to touchstart it should be like that:
document.addEventListener('touchstart', handler, {passive: true});
This way you can setup in advance which exact event and if the passive interface is supported:
var passiveEvent = false; try { var opts = Object.defineProperty({}, 'passive', { get: function () { passiveEvent = true; } }); window.addEventListener("test", null, opts); } catch (e) { } // in my case I need both passive and capture set to true, change as you need it. passiveEvent = passiveEvent ? { capture: true, passive: true } : true; //if you need to handle mouse wheel scroll var supportedWheelEvent: string = "onwheel" in HTMLDivElement.prototype ? "wheel" : document.onmousewheel !== undefined ? "mousewheel" : "DOMMouseScroll";
And use like this:
elementRef.addEventListener("touchstart", handler, passiveEvent);
More details on passive event listeners here: https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With