1

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) });
}
Oriesok Vlassky
  • 797
  • 1
  • 13
  • 26

2 Answers2

1

You can also try to use .once method as:

yxMap.events.once('click', handler, context);
Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
dimik
  • 76
  • 2
0

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);
Jan Jongboom
  • 26,598
  • 9
  • 83
  • 120