简体   繁体   English

雄辩的JavaScript中的removeEventListener示例

[英]removeEventListener example from Eloquent JavaScript

In the following screenshot, can someone please explain why you have to pass the function "once" to button.removeEventListener("click", once)? 在下面的屏幕截图中,有人可以解释为什么您必须将函数“一次”传递给button.removeEventListener(“ click”,一次)吗? Do we merely pass it because the removeEventListener method requires two arguments? 我们是否仅通过removeEventListener方法需要两个参数来传递它? Additionally, it seems strange that "Done" is not console logged more than once given the "once" function is also passed into the removeEventListener method. 另外,给定“一次”功能也传递到removeEventListener方法中,“完成”没有多次被控制台记录似乎很奇怪。

在此处输入图片说明

 let button = document.getElementById("button"); function once() { console.log("Done"); button.removeEventListener("click", once); } button.addEventListener("click", once); 
 <button id="button">once</button> 

When you want to unbind only specific handler (like here you are unbinding once handler), you need to pass that as the second parameter, otherwise JS would not know which handler to remove. 当您只想取消绑定特定的处理程序时(例如此处要取消绑定once处理程序),则需要将其作为第二个参数传递,否则JS将不知道要删除哪个处理程序。

There can be multiple handlers bound to each event. 每个事件可以绑定多个处理程序。

Additionally, it seems strange that "Done" is not console logged more than once given the "once" function is also passed into the removeEventListener method. 另外,给定“一次”功能也传递到removeEventListener方法中,“完成”没有多次被控制台记录似乎很奇怪。

That's the reason why it's called only once. 这就是为什么只调用一次的原因。 You are passing a reference of function once there, so JS knows which handler to unbind. 您传递的函数的引用once在那里,所以JS知道要解除绑定该处理程序。 It does not call it when you call removeEventListener . 当您调用removeEventListener时,它不会调用它。

The function is called once user clicks the button, in the handler there is this console.log , and right after it will unregister itself so latter clicks will not fire that function anymore. 一旦用户单击该按钮,该函数便被调用,在处理程序中有该console.log ,并且此后它将立即注销自身,因此以后的单击将不再触发该函数。

You can bind multiple events in javascript to an element on the same action. 您可以将javascript中的多个事件绑定到同一操作上的一个元素。

When you want to remove a particular handler from the bound events you need to pass that handler function name in removeEventListener . 当您想从绑定事件中删除特定处理程序时,需要在removeEventListener传递该处理程序函数名称。 If you do not pass the second argument in removeEventListener , it will simply remove all the handlers for that action on that event. 如果您没有在removeEventListener传递第二个参数,它将仅删除该事件上该动作的所有处理程序。

In your case, you are removing the handler once from within itself. 在您的情况下,您要从自身内部删除处理程序once

An example with different handlers to illustrate: 一个带有不同处理程序的示例来说明:

 function firstListener(e) { console.log('firstListener'); } function secondListener(e) { console.log('secondListener'); // this will remove `firstListener` handler from subsequent button clicks e.target.removeEventListener('click', firstListener); } // bind first event handler function document.querySelector('#btnMultiEvent').addEventListener('click', firstListener); // bind second event handler function document.querySelector('#btnMultiEvent').addEventListener('click', secondListener); 
 <button id="btnMultiEvent">Multiple Event Button</button> 

More on removeEventListener 有关removeEventListener的更多信息

Let me explain in an ascii code table: 让我在ascii代码表中进行解释:

               | script start | nothing happens | once() execute | nothing happens |
---------------|--------------|-----------------|----------------|-----------------|
"click" Events | once() added | once() waiting  | once() removed |                 |
               | lala() added | lala() waiting  | lala() waiting | lala() waiting  |

The function lala is the same as once and removes it self from the "click" Events. 函数lala与一次函数相同,并将其自身从“ click”事件中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM