How to disable events for a while in Javascript or jQuery

I am wondering if you know that you have a different approach to disabling an event for a while. Let me elaborate on this:

Say I have a div or button that has a subscriber to an onclick event. To prevent double-clicking when methods perform some ajax actions, these are some of the ways we can do this:

  • Disable the button until the method completes its task
  • Untie it until the methods finish its work, and then bind it again.
  • Use some kind of flag system like boolean, so it will interfere with the method more than once.

So, are there any other ways, maybe some javascript tricks or jQuery tricks that are more efficient and better.

Thanks in advance.

+4
source share
3 answers

I will just add a class such as 'disabled' for this div or button. And in my function registered in the onclick event, I check if this class is present. If so, just come back.

I can’t think of anything else other than what you said.

+4
source

I think the boolean flag is a pretty elegant solution, and you can keep it “contained” using a handler property, for example:

 $(someElement).click(myHandler); function myHandler() { if (!myHandler.inProgress) { myHandler.inProgress = true; // Do stuff // Set it back to false later } } 
+1
source

I cannot come up with a more “complex” or “elegant” solution than the ones you indicated.

what is so inefficient when disconnecting an element or removing a binding?

0
source

Source: https://habr.com/ru/post/1306088/


All Articles