jQuery bind() unbind() and on() and off()

后端 未结 3 868
夕颜
夕颜 2021-02-10 05:05

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

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-10 06:07

    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

提交回复
热议问题