Lieutenant Dan
Lieutenant Dan

Reputation: 8274

Click Event is fired multiple times at the same time

Within a click function, I have the below code:

home.addEventListener('click', () => {

       myAPP.mapview.map.layers.items.forEach(item => {
            item.visible = false;
        });
       //........... }

However, after the button is clicked once, it seems to continuously run/execute. How could I assure it executes, i.e. 'resets' just one time, just when the button or click function occurs?

Upvotes: 1

Views: 240

Answers (1)

Mohamed Magdy
Mohamed Magdy

Reputation: 605

The problem you are having is that multiple dom elements are firing the same click event at the same time. You should have the following:

home.addEventListener('click', (e) => {
  // to prevent child/parent dom elements from firing the same event.
  if( e.target !== this ) {
    return;
  }
});

Upvotes: 1

Related Questions