Block a function when other called

Hello there. Iam stuck with a javascript issue i have for days. I tried many solutions i found but nothing seems to work properly as i want. Let me explain the problem. So i have a google map on my website and a marker . I also have 2 functions for this marker. The first should work in normal conditions and the second one should work when the marker have been dragged.But when the second function is triggered (aka marker dragged) the first function should run no more. Like it never existed. I tried the method where you put a property object in function ,set it true (drag.called=true;) and then when you want to block the other function ,you put an if statement like this. if (!drag.called) { …} . This doesnt work properly. Because the drag.called isnt defined at first , the first function isnt working. So to summarize. Function notdragged must work , then drag function is called and notdragged should not work anymore. Can you tell me what to do? I can provide an example.

Could you please provide the code that you used? Make sure you format it properly by placing three backticks (`) on the line before the code and three backticks on the line after the code.

searchMarker.on('dragend', function drag(e) {
      ...
            drag.called = true;
      ...
});

if (!drag.called) {
$("#player-button").on('click', function() {
   ...
   ...
});
}

You need to do the check on runtime, not on definition time. e.g.

searchMarker.on('dragend', function drag(e) {
      ...
      searchMarker.called = true;
      ...
});
$("#player-button").on('click', function() {
   if (searchMarker.called) {
      return true;
   }
   ...
});
2 Likes

… and if you want to set a property on the drag function itself (which is quite possible), you’d have to define the function outside the .on() call to make it available to the other function. If you check the console you’ll see an uncaught reference error otherwise.

1 Like

It worked and both replies were very educational. Thank you very much. Solved!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.