jQuery bind()unbind()和on()和off() [英] jQuery bind() unbind() and on() and off()
问题描述
解除事件以提高性能(客户端)是否有意义?或者,再次绑定事件并绑定30秒吗?
我的问题:
bind() - unbind()或on()。off()只是增加基于客户端的性能,还是应该用于其他场景?这个问题是因为我的JavaScript代码正在增长和增长(约30%),因为解除绑定的事件。而我认为,有些事情可能无法正常工作,当用户没有交互时,我想要...
。
编辑:最重要的是绑定/解除绑定的按键事件,因为我需要用于差异的箭头键。
解除绑定只是为了性能原因重新绑定可能是错误的,并且在大多数情况下使事情过于复杂。 / p>
而不是在许多特定的DOM元素上绑定事件监听器,您可以采用更多的鸟瞰方法,并在DOM树顶部附近绑定几个监听器,那么当事件被触发时,检查实际点击的内容。
这样你不会花费CPU来绑定/解绑定大量的事件侦听器,而是使用一个小的CPU当一个事件被处理时(通常不明显)就会触发。
这里详细介绍:事件委托vs向页面添加复杂元素时的直接绑定
Im working on a small adminarea for a webpage.
Does it make sense to unbind events for increasing performance(client)? Or does it cost more performance to unbind events and binding it 30Seconds later again?
My questions: Is the idea behind bind()-unbind() or on().off() just increasing clientbased performance or should i use it for other scenarios? This question comes because my javascript code is growing and growing (about 30%) because of unbinding events. And i think, that some things may not work, when user interacts not, as i want...
.
EDIT: The most times im binding/unbinding keypress events, because i need the arrow keys for diff. scenarios.
Unbinding only to bind again for performance reasons is probably bug-prone and makes things overly complicated in most cases.
Instead of binding event listeners on many specific DOM elements, you could take a more "birds eye" approach and bind just a few listeners near the top of the DOM tree, and then when the event is triggered check what was actually clicked.
That way you won't spend CPU on binding/unbinding lots of event listeners, but instead take a small CPU hit when an event is processed (which is usually not noticeable).
This is covered in detail here: event delegation vs direct binding when adding complex elements to a page
这篇关于jQuery bind()unbind()和on()和off()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!