i am currently on a script to controls car doors, window, etc. At the first time its works fine, but when i close it, then repoen it, the ui seem to reopen itself one more time, like, when i open it the second time and click a button 2 times, then when i open it 3 times, it detect the click 3 times and so on.
here is a code sample if anyone can help me out, i would appreciate it.
if (item.status == true){
$(“#container”).css(“display”,“block”);
$(“#door1”).click(function(){
console.log(“im a click”);
if (!isDoor1On){
$(“#imgDoor1”).attr(“src”,“img/doorFrontRightActivated.png”);
isDoor1On = true;
}else{
$(“#imgDoor1”).attr(“src”,“img/doorFrontRight.png”);
isDoor1On = false;
}
value = 1;
$.post(“https://DTZ_vehControls/door1”,JSON.stringify({value}));
return
Without seeing the full code, this is my best guess:
$(“#door1”).click(function(){ at line 3 is called everytime you open the container. This puts a listener on #door1 everytime you open the UI and the listener does not stop listening when you close the UI.
The solution to this is putting any listeners outside code that will be executed multiple times (i.e. when opening the UI).
if (item.status == true) {
$(“#container”).css(“display”,“block”);
} else {
...
}
$(“#door1”).click(function() {
console.log(“im a click”);
if (!isDoor1On){
$(“#imgDoor1”).attr(“src”,“img/doorFrontRightActivated.png”);
isDoor1On = true;
} else {
$(“#imgDoor1”).attr(“src”,“img/doorFrontRight.png”);
isDoor1On = false;
}
value = 1;
$.post(“https://DTZ_vehControls/door1”,JSON.stringify({value}));
return
});