+ 2

Javascript | How to prevent events to execute multiple times, until unless its done for once.

So, i have a button, which is for hiding or showing a div. Its working fine but when i rapidly click on that button , for multiple times. The div keeps showing and hiding for secs, even though i finished clicking on it. I think its happening because due of clicking rapidly, which calls the event listener method multiple times. Any solution?

2nd Jan 2020, 6:42 AM
Saurav Kumar
Saurav Kumar - avatar
3 odpowiedzi
+ 7
You could create event trigger once with following btn.addEventListener("click", clickFn, {once: true}); And also add this event trigger once at the end of clickFn function, so triggering would be only set once whenever the clickFn has done the work.
2nd Jan 2020, 8:29 AM
Calviղ
Calviղ - avatar
+ 3
You can add a boolean variable that waits a certain amount of time before each click
2nd Jan 2020, 6:51 AM
Rain
Rain - avatar
+ 1
What!! I didn't know that. That's super cool. I really need to learn about event triggers.
2nd Jan 2020, 7:16 PM
Rain
Rain - avatar