1

I don't know how to break or return from addEventListener

Here I get "Undefined label 'testfunc'" error

testfunc:
    test.addEventListener("click", function(){
        console.log("TEST")
        break testfunc
    })

Here I get "TEST" everytime i click but I want it only to console.log("TEST") only once

function testfunc(){
    test.addEventListener("click", function(){
        console.log("TEST")
        return
    })
}
5

1 Answer 1

-3

you can create some variable and put there flag (true/false), for example:

let isClicked = false;

function testfunc(){
    test.addEventListener("click", function(){
      if (isClicked) return;
        console.log("TEST")
       isClicked = true
    })
}

1
  • @LukaZmaj Sure it works, but it makes more sense to stop the event completely from firing.
    – Ivar
    Jan 26, 2022 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.