How to prevent calling of en event handler twice on fast clicks?

前端 未结 9 1020
小鲜肉
小鲜肉 2020-12-15 19:23

There is a button and when user clicks on button, some data is saved to back-end. Issue is when user clicks on button very quickly, event handler is getting executed multipl

9条回答
  •  独厮守ぢ
    2020-12-15 20:08

    Assuming you attached and event listener to the button, one approach ist to just remove the event listener from the button and attach it afterwards again if needed.
    In the event listener function itself add the following code:

    function handleButtonClick(e){
      e.target.removeEventListener("click", handleBackButton);
    }
    

    Depending on your setup this could work.

提交回复
热议问题