admin管理员组文章数量:1430560
Is it considered 'good practice' to stop JavaScript event propagation - does it benefit performance in any way?
I'm wondering if there is benefit outside of layout purposes where you stop propagation so you don't trigger multiple events accidentially.
Is it considered 'good practice' to stop JavaScript event propagation - does it benefit performance in any way?
I'm wondering if there is benefit outside of layout purposes where you stop propagation so you don't trigger multiple events accidentially.
Share Improve this question asked Sep 23, 2009 at 7:35 AlexAlex 77.5k91 gold badges265 silver badges350 bronze badges 2- Even if it does, it's squarely within the realm of micro-optimization. It's nothing you would notice in normal use. – slikts Commented Sep 23, 2009 at 7:45
- This does not explain the benefit of stopping event propagation, but it explains the price you have to pay for it: css-tricks./dangers-stopping-event-propagation – ceving Commented Jan 9, 2024 at 9:49
2 Answers
Reset to default 6It entirely depends on the DOM plexity and the actions taken in each event that is triggered.
You would stop propagating events mainly not because of plexity but because of unwanted actions not to take place.
For example: Consider a situation where you have a show hide div
and when you click on the div
it should open and if you click anywhere else in the document it should close.
So you would wire an onclick
event handler to the document as well as the div
. So you have to stop propagating the event when clicked on the div
so that the document click handler won't be invoked. That's a situation where you use stop event propagation.
Only when it is necessary to the program logic, such as to prevent form submission or another DOM element's action being triggered. Unless you have a seriously plex DOM tree, there will not be any serious performance hit.
本文标签: JavaScript Is there any performance benefit to stopping event propagationStack Overflow
版权声明:本文标题:JavaScript: Is there any performance benefit to stopping event propagation? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745438763a2658348.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论