简体   繁体   中英

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, ie 'resets' just one time, just when the button or click function occurs?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM