最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - OpenLayers 3: Remove event listener - Stack Overflow

programmeradmin3浏览0评论

In Openlayers 3 how to remove a event listener attached like this:

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?

In Openlayers 3 how to remove a event listener attached like this:

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?

Share Improve this question edited Aug 27, 2015 at 17:52 Shaunak asked Aug 27, 2015 at 15:16 ShaunakShaunak 18k5 gold badges56 silver badges86 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

Ah its pretty simple! Its in the API Docs: unByKey, but very counter-intuitive name for an off function.

So to remove the event listener a:

map.unByKey(a);

Will remove a listener but keep the b on.

Note: this will work across any object in Open Layers 3 that emits an event. like Layers, Interactions etc..

I was just wondering the same thing, seems like off would be the appropriate method to remove an event listener. 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.

发布评论

评论列表(0)

  1. 暂无评论