Five M script problem

Hi everyone,

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
});
1 Like

Actually this make a lot of sens and i havnt taught about that, i will try it and give you back news. Thx for your point mate!

it’s works! Thanks again dude! I really appreciate it!!

1 Like

@MaN11aC

Consider marking his response as the solution to help future forum readers find the answer with ease :slight_smile:

i just did.

sorry about that!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.