英文:
How to prevent event listener getting called twice
问题
以下是要翻译的内容:
-
在Highcharts的
render
方法中,通过类名检索到的HTML元素上添加了事件监听器,但是该监听器被调用了两次。请检查下面的代码,其中点击事件被调用。 -
在Highcharts的
xAxis
标签格式化程序中,在x轴上添加了链接。
-
在Highcharts的
render
中添加了onClick
处理程序。
但不知何故,此处理程序被调用了两次。我已经尝试了多种解决方案,但都没有奏效。
解决方案:
- 添加了
e.stopPropagation()
和e.preventDefault()
,但没有奏效。 - 下面的
{once: true}
解决方案也没有奏效:
label.addEventListener('click', (e) => {
console.log('click event');
}, {once: true});
- 即使删除了事件监听器,点击函数仍然被调用。
请问是否有人可以提供解决方法,以避免事件监听器被调用两次。
英文:
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.
But somehow this handler is getting called twice. I have added below multiple solutions but it doesn't work.
Solutions:
-
Added
e.stopPropagation()
ande.preventDefault()
but didn't work. -
Below {once: true} solution didn't work
label.addEventListener('click', (e) => {
console.log('click event');
}, {once: true}); -
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/
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 });
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论