[SOLVED] How can I break or return from addeventlistener

Issue

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
    })
}

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
    })
}

Answered By – Pavlo Marchenko

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published.