如何防止事件监听器被调用两次

huangapple go评论82阅读模式
英文:

How to prevent event listener getting called twice

问题

以下是要翻译的内容:

  1. 在Highcharts的render方法中,通过类名检索到的HTML元素上添加了事件监听器,但是该监听器被调用了两次。请检查下面的代码,其中点击事件被调用。

  2. 在Highcharts的xAxis标签格式化程序中,在x轴上添加了链接。
    如何防止事件监听器被调用两次

  3. 在Highcharts的render中添加了onClick处理程序。
    如何防止事件监听器被调用两次

但不知何故,此处理程序被调用了两次。我已经尝试了多种解决方案,但都没有奏效。

解决方案:

  1. 添加了e.stopPropagation()e.preventDefault(),但没有奏效。
  2. 下面的{once: true}解决方案也没有奏效:
label.addEventListener('click', (e) => {                
   console.log('click event');
}, {once: true});
  1. 即使删除了事件监听器,点击函数仍然被调用。

如何防止事件监听器被调用两次

请问是否有人可以提供解决方法,以避免事件监听器被调用两次。

英文:

Added event listener on a HTML element which is retrieved by className in the render method of highcharts but that listener is getting called twice. Please check below code where click event gets called.

  1. Added link on xAxis of highchart in xAxis label formatter.
    如何防止事件监听器被调用两次

  2. Added onClick handler in the render of highchart
    如何防止事件监听器被调用两次

But somehow this handler is getting called twice. I have added below multiple solutions but it doesn't work.

Solutions:

  1. Added e.stopPropagation() and e.preventDefault() but didn't work.

  2. Below {once: true} solution didn't work

    label.addEventListener('click', (e) => {
    console.log('click event');
    }, {once: true});

  3. Removed event listener still click function get called.

如何防止事件监听器被调用两次

Can someone please provide solution to avoid event listener getting called twice.

答案1

得分: 1

那是因为render事件可以被触发多次,导致添加多个事件监听器。根据Highcharts API:

render

在图表初始加载后触发(在load事件之后),以及在每次重绘后触发(在redraw事件之后)。

作为解决方法,您可以使用另一种方式来添加事件监听器。通过使用custom-events插件或on方法,回调函数将仅触发一次。

chart: {
  events: {
    render: function() {
      const xAxis = this.xAxis[0];
      const ticks = xAxis.ticks;

      xAxis.tickPositions.forEach(tickPos => {
        ticks[tickPos].label.on('click', () => {
          console.log('点击')
        });
      });
    }
  }
}

在线演示: http://jsfiddle.net/BlackLabel/uo8hc526/

文档: https://developer.mozilla.org/zh-CN/docs/Web/API/EventTarget/addEventListener#多个相同的事件监听器

API 参考:

https://api.highcharts.com/highcharts/chart.events.render

https://api.highcharts.com/class-reference/Highcharts.SVGElement#on

英文:

That's because render event can be fired multiple times which results in multiple event listeners added. From Highcharts API:

> render
>
> Fires after initial load of the chart (directly after the load event),
> and after each redraw (directly after the redraw event).


As a solution, you can use another way to add the event listeners. By using custom-events plugin or on method, a callback function will be fired only once.

  chart: {
    events: {
      render: function() {
        const xAxis = this.xAxis[0];
        const ticks = xAxis.ticks;

        xAxis.tickPositions.forEach(tickPos => {
          ticks[tickPos].label.on('click', () => {
            console.log('CLICK')
          });
        });
      }
    }
  }

Live demo: http://jsfiddle.net/BlackLabel/uo8hc526/

Docs: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Multiple_identical_event_listeners

API Reference:

https://api.highcharts.com/highcharts/chart.events.render

https://api.highcharts.com/class-reference/Highcharts.SVGElement#on

答案2

得分: 0

你可以在方法选项对象中传递 once,这将在首次触发事件时移除 eventListener,例如

let btn = document.querySelector("button")
btn.addEventListener('click', () => {
    console.log("已记录");
}, { once: true });
英文:

You can pass once in method options object this will remove the eventListener when it'll fire first time for eg

let btn = document.querySelector("button")
btn.addEventListener('click', () => {
    console.log("Logged");
}, { once: true });

huangapple
  • 本文由 发表于 2023年2月27日 14:53:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定