简体   繁体   English

如何删除使用匿名函数传递参数的事件侦听器?

[英]How do you remove an event listener that uses an anonymous function for passing parameters?

Apologies in advance as I have done some searching and this appears to be a fairly common question, but none of the answers I have found quite meet my needs. 事先道歉,因为我做了一些搜索,这似乎是一个相当普遍的问题,但我找到的答案都没有满足我的需求。 The closest I was able to find was How do I add and remove an event listener using a function with parameters? 我能找到的最接近的是如何使用带参数的函数添加和删除事件监听器? , but the answer there involves JQuery and I am trying to find a way to do this in just JS. ,但那里的答案涉及JQuery,我试图在JS中找到一种方法来做到这一点。

I am using anonymous functions to pass parameters through an event trigger which I believe is the correct way to do so. 我使用匿名函数通过事件触发器传递参数,我认为这是正确的方法。 If temp is defined by some calculations based on the state at the time the event is added, I want to add the listener as follows: 如果temp是根据添加事件时的状态通过某些计算定义的,我想按如下方式添加监听器:

item.addEventListener("click", function(){myOnClickFunction(temp)});

However, I want to be able to remove the event dynamically if certain conditions are met. 但是,如果满足某些条件,我希望能够动态删除事件。

item.removeEventListener("click", function(){myOnClickFunction(temp)});

This does not work as the inline function is anonymous and cannot be referenced for matching up the event listener (plus temp is likely different anyway). 这不起作用,因为内联函数是匿名的,并且无法引用以匹配事件监听器(加上temp可能不同)。 Since temp is calculated at the time of the trigger, I cannot store a reference outside of the anonymous function with something like this: 由于temp是在触发时计算的,因此无法在匿名函数之外存储引用,如下所示:

var listener = function() {
  var temp = calculate(arg1, arg2, event);
  myFunction(temp);
};
window.addEventListener('click', listener, false);

so that I can later call: 以便我以后可以打电话:

window.removeEventListener('click', listener, false);


myOnClickEvent(temp){
  //My code and calculations here
  document.removeEventListener("keypress", arguments.callee);
}

is also not working, although I'm less confident as to how that method is supposed to work. 也没有用,虽然我对这种方法应该如何工作缺乏信心。

How can I remove this function dynamically? 如何动态删除此功能? In some cases, I might be able to refactor my code so that all variables that need to be passed are stored globally and rewritten constantly, but that sounds like messy code. 在某些情况下,我可能能够重构我的代码,以便所有需要传递的变量全局存储并不断重写,但这听起来像是凌乱的代码。 Plus, in some cases the trigger event is one of the arguments that needs to be passed so I don't think I could make that happen. 另外,在某些情况下,触发事件是需要传递的参数之一,因此我认为我无法实现这一点。 Any ideas? 有任何想法吗?

You can create an object, whose properties are the temp values, and whose values are myOnClickFunction bound to that temp . 您可以创建一个对象,其属性是temp值,其值是绑定到该temp值的myOnClickFunction For example: 例如:

const boundFns = {};
// acquire temp somehow
const boundFn = () => myOnClickFunction(temp);
boundFns[temp] = boundFn;
item.addEventListener("click", boundFn);

Then, when you need to remove the listener, retrieve the appropriate bound function: 然后,当您需要删除侦听器时,检索适当的绑定函数:

item.removeEventListener("click", boundFns[temp]);

If a temp may be used more than once, check if it exists in boundFns first: 如果temp可以多次使用,请先检查boundFns是否存在:

const boundFns = {};
// acquire temp somehow
if (!boundFns[temp]) {
  boundFns[temp] = () => myOnClickFunction(temp);
}
const boundFn = boundFns[temp];
boundFns[temp] = boundFn;
item.addEventListener("click", boundFn);

If temp cannot be be used reliably as a unique object key (for example, if it's an HTMLElement), you can use a Map instead, which is like an object, but whose keys can be anything , not just strings: 如果temp不能被可靠地用作唯一对象键(例如,如果它是HTMLElement),则可以使用Map ,它就像一个对象,但其键可以是任何东西 ,而不仅仅是字符串:

const boundFns = new Map();
boundFns.set(temp, boundFn);
// ...
item.removeEventListener("click", boundFns.get(temp));

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

相关问题 如何删除在TypeScript中使用“this”的事件侦听器? - How do you remove an event listener that uses “this” in TypeScript? 如何删除使用箭头函数的事件的事件侦听器 - How do I remove an event listener on an event that uses an arrow function 如何使用带参数的函数添加和删除事件侦听器? - How do I add and remove an event listener using a function with parameters? 如果 function 有参数,如何删除事件监听器? - How to remove event listener if function has parameters? 如何删除作为匿名函数传递的事件侦听器? - How do I remove an event listener that has been passed in as an anonymous function? 如何在不命名的情况下从匿名函数中删除事件监听器? - How to remove event listener from anonymous function without naming it? 如何从具有匿名功能的div中删除事件侦听器 - How to remove an event listener from a div that has an anonymous function 如何使用带有事件参数和参数的函数添加和删除事件侦听器? - How do I add and remove an event listener using a function with event argument and parameters? 将参数传递给Click事件中的Anonymous函数 - Passing parameters to Anonymous function inside click event 将参数传递给JavaScript中的事件监听器函数 - Passing parameters to a event listener function in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM