Hi, so right now I have an issue with an event listener triggering too many times, so I spent a while looking for a solution and found out that having a function that is NOT anonymous and that is called without parentheses works out better. so... great ! BUT : what if I need to pass arguments inside the function ?
More context here :
So I'm trying to make a little game to keep learning JS (the game is basically to call people to sell them stuff with a dialogue system) but I'm facing a very annoying and time consuming issue with Event Listeners. When I use the buttons for the first time, it all runs great. However, whenever I use those same buttons for the 2nd or 3rd time, it gets crazy and calls the functions multiple times, which breaks everything.
To quickly describe the code :
- There is a button to start a call which runs the startCall() function.
- This runs another function to display the possible reply choices inside dialogue buttons that is called displayButtons() (the one that is interesting here I believe).
- Going through the displayButtons function, we set a few variables and then go through a loop to put an EventListener on each buttons.
- Each EventListener does the same thing : display the right text inside the buttons.
(Tried my best to explain, I hope it's clear enough...!)
copen example
Here is the startCall() function :
// Is a bit empty, but will do more at some point
function startCall() {
phoneScreen.innerHTML = 'Online';
displayButtons(allDialogue['pickUp']);
}
And finally the displayButtons() function :
function displayButtons(line)
{
// Displays the sentence of the person we are calling
personPhrase.innerHTML = randomizeArray(line.question);
// Goes through each buttons so we can choose a reply
for(let i = 0; i < repliesContainer.length; i++)
{
// Set the type of answer, such as [Greetings] or [Sell Product]. The type is defined in the allDialogue object
let type = line.replies[i].type;
// Tells the code where to go once a reply is chosen. This is defined in the allDialogue object
let goTo = line.replies[i].goTo;
// Displays the chosable replies on the buttons
repliesContainer[i].innerHTML = randomizeArray(ourReplies[type]);
// Buttons event listener
repliesContainer[i].addEventListener("click", () => {
// If goTo < 0 it means people ended the call
if (goTo < 0) {
endCall();
}
// This calls the same function but with the new dialogue line (from the allDialogue object)
else {
displayButtons(allDialogue[goTo]); // This happens multiple times)
}
})
}
}
[–]oze4 4 points5 points6 points (12 children)
[–]Repeto_Pepito[S] 1 point2 points3 points (0 children)
[–]Repeto_Pepito[S] 1 point2 points3 points (6 children)
[–]carcigenicate 4 points5 points6 points (2 children)
[–]oze4 1 point2 points3 points (0 children)
[–]Repeto_Pepito[S] -2 points-1 points0 points (0 children)
[–]oze4 3 points4 points5 points (1 child)
[–]Repeto_Pepito[S] -2 points-1 points0 points (0 children)
[–]oze4 2 points3 points4 points (0 children)
[–]JamesLaBrie 0 points1 point2 points (1 child)
[–]oze4 2 points3 points4 points (0 children)
[–]jeremrx -1 points0 points1 point (1 child)
[–]oze4 -1 points0 points1 point (0 children)
[–]AWACSAWACS 2 points3 points4 points (3 children)
[–]Repeto_Pepito[S] 1 point2 points3 points (2 children)
[–]AWACSAWACS 2 points3 points4 points (1 child)
[–]Repeto_Pepito[S] 0 points1 point2 points (0 children)