简体   繁体   English

OpenLayers 3:删除事件监听器

[英]OpenLayers 3: Remove event listener

In Openlayers 3 how to remove a event listener attached like this: Openlayers 3中如何删除附加的事件侦听器,如下所示:

var a = map.on("pointerdrag",function (e) {
             // event handler
});

var b = map.on("pointerdrag",function (e) {
             // event handler
});

How do I remove only listner a and keep b active? 如何删除只是听者中a和保持b活跃?

Ah its pretty simple! 啊它非常简单! Its in the API Docs : unByKey , but very counter-intuitive name for an off function. 它在API DocsunByKey ,但对于off函数非常反直觉。

So to remove the event listener a : 因此,要删除事件监听器a

map.unByKey(a);

Will remove a listener but keep the b on. 将删除a监听器但保持b

Note: this will work across any object in Open Layers 3 that emits an event. 注意:这将适用于发出事件的打开图层3中的任何对象。 like Layers, Interactions etc.. 喜欢图层,交互等。

I was just wondering the same thing, seems like off would be the appropriate method to remove an event listener. 我只是想知道同样的事情,似乎off将是删除事件监听器的适当方法。 You can also call this directly in the event callback: 您也可以在事件回调中直接调用它:

map.on("pointerdrag", function (e) {
    map.unByKey(e);
});

This should remove only the this specific event. 这应该只删除此特定事件。

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

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