简体   繁体   中英

Yandex maps api - event handler executed only once

I would like to achieve something similar as .one() / http://api.jquery.com/one/ with myYandexMap.events.add('click', function(e){}) . That means, to have a function on click, that would only be run once or I could replace it with other function.

I have tried to remove the inner function at the end of the function, but I didn't figure out how to do it. This is basically what I've done

yxMap.events.add('click', function (e){ add_wp_map_click(e, num, color) });

function add_wp_map_click(e, num, color){
  ...
  yxMap.events.remove('click', function (e){ add_wp_map_click(e, num, color) });
}

您也可以尝试将.once方法用作:

yxMap.events.once('click', handler, context);

You have to remove the same handler, redeclaring the function doesn't work because it are two different functions for javascript ( function() {} === function() {} -> false ).

Try:

var clickHandler = function(e) { 
    yxMap.events.remove('click', clickHandler);

    add_wp_blah(); 
};

yxMap.events.add('click', clickHandler);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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