+ 1

Js buttons

I have button in my code it run a function but when you clicked more than once it run again and again How to prevent that

10th Dec 2022, 11:38 AM
YaSer
3 Answers
+ 5
you can also add a {once:true}. const btn = document.getElementById("btn"); btn.addEventListener("click",handleClick, {once:true}); function handleClick() { console.log("btn clicked"); }
10th Dec 2022, 2:35 PM
Bob_Li
Bob_Li - avatar
+ 3
You can remove event listener inside this function: https://code.sololearn.com/Wqfi62H89Plx/?ref=app
10th Dec 2022, 1:58 PM
PanicS
PanicS - avatar
+ 2
You can put the condition in your function, like: initialize a variable with true and put a if condition inside your function and at the end of the function reassign the same variable to false.
11th Dec 2022, 8:00 PM
Affan Ali
Affan Ali - avatar