I have a drop down that has a changed event attached. When it changes it fires multiple times. 2 times when previewing and 3 times on the live site. I had issues were i kept having to go back and re attach the handler because they disappear from the control all the time. I’m wondering if the back end added a new event handler each time i did that. The wix support people would not even hear my question because there was code on the page. Is this not their problem? How is the user supposed to control the number of times the events trigger?
It can be that something is wrong in your code, and it can be Wix’s bug (for example radioButtonGroups and checkboxGroups fire twice when clicked).
Maybe post your code here?
and anyway, instead of using export function, you can use:
$w("#element").onChange((event) => {
})
Put it inside $w.onReady().
This method is more resistant to bugs and mistakes
I’ve removed all code except for comments and it still happens. I will take your advice but its happening all over the site which is really annoying. Thank you for your input.
@johnschruben you’re welcome. It happened a while ago on duplicated pages, and got fixed.
Maybe there’s a bug again.
@jonatandor35 Ha it was a copied from another page.
This happened to me. My code was inserting records into tables and sending email. I ended up with double inserts and double the email. Some of the email was duplicate invoice being sent to customers. Needless to say, it caused problems with my code and my clients.
I solved the problem by sidestepping the unpredictability of Wix’s unknown and undocumented code. I declared a module-wide variable that is used inside a specific event, assigning it a value of FALSE. When the event fires the first time, the variable is tested. If it is false, it is changed to TRUE, and the code executes as intended. The next time it executes, its value is true, and the code is not executed. It looks like this:
var button5Clicked = false;
…
…
export function button5_click(event) {
if (button5Clicked === false) {
button5Clicked = true;
// code to execute goes here
} else {
// do nothing
}
}
The else clause is unnecessary; it was included here only for didactic purposes.
This behavior of executing more than once is the kind of thing that needs to be documented along with a good explanation as to why it is necessary.