79562306

Date: 2025-04-08 14:23:38
Score: 0.5
Natty:
Report link

I had similar issue even when calling function properly. I've added event listener after clicking on some button and function was calling immediately. It was due to race conditions and event bubbling. Parent button click event haven't finished bubbling and was picked of this just added event listener. After adding setTimeout for 0 milliseconds it was fixed:

onSomeButtonClick(event) { // function that is fired after button click
    this.setEventListener(); // add event listener on click 
}

    setEventListener() {
        setTimeout(() => {
            this.document.addEventListener(
                'click',
                this.handleOutsideClick // function I want to fire after click
            );
        }, 0);
    }
Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Igor Vasylevskyi