Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I break or return from addeventlistener

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
    })
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading