[Javascript] Remove an Event Listener with removeEventListener

removeEventListener removes an event listener added with addEventListener. However, there are a number of gotchas to watch out for in order to correctly remove an event listener.

Sometime, if you passed third options to addEventListener, but you forgot to pass the same option when calling removeEventListener, the event won't be removed successfully

 

Simple helper:

export default function bind(
  target, {type, listener, options}
) {
    target.addEventListener(
        type,
        listener,
        options,
    );

    return function unbind() {
        target.removeEventListener(
            type,
            listener,
            options,
        );
    }
}

 

posted @ 2021-06-14 15:01  Zhentiw  阅读(70)  评论(0编辑  收藏  举报