AmCharts 3-首次运行时删除监听器(其内部的引用函数)

时间:2019-04-01 14:27:49

标签: javascript amcharts

我想知道如何添加仅运行一次的侦听器,我相信最终这主要是一个通用的javascript问题,但是我可能会遗漏一些东西。

这就是我所拥有的:

const listener = chart.addListener('rendered', () => {
    console.log('Chart rendered! This should only print once!');
    doSomethingToChart(chart);
    chart.removeListener(chart, 'rendered', listener);
})

这里的问题是,没有删除侦听器,console.log在每个渲染器上打印。我该如何解决?

2 个答案:

答案 0 :(得分:2)

addListener根本没有返回值,也没有提及一个in the documentation,这就是为什么您的代码不起作用的原因。只需在添加列表器之前单独存储函数引用,然后在该引用上调用removeListener即可,例如:

let counter = 0;
const listener = () => {
  console.log('dataUpdated called', counter += 1);
};

chart.addListener('dataUpdated', listener);

let timer = setInterval(() => {
  chart.validateData(); //triggers dataUpdated
  if (counter == 3)  {
    chart.removeListener(chart, 'dataUpdated', listener);
    console.log('dataUpdated removed')
    chart.validateData(); //one more call to confirm that the listner no longer runs
    clearInterval(timer);
  }

}, 1500);

演示:

var chart = AmCharts.makeChart("chartdiv", {
  "type": "serial",
  "theme": "light",
  "dataProvider": [{
    "country": "USA",
    "visits": 2025
  }, {
    "country": "China",
    "visits": 1882
  }, {
    "country": "Japan",
    "visits": 1809
  }, {
    "country": "Germany",
    "visits": 1322
  }, {
    "country": "UK",
    "visits": 1122
  }, {
    "country": "France",
    "visits": 1114
  }, {
    "country": "India",
    "visits": 984
  }, {
    "country": "Spain",
    "visits": 711
  }, {
    "country": "Netherlands",
    "visits": 665
  }, {
    "country": "Russia",
    "visits": 580
  }, {
    "country": "South Korea",
    "visits": 443
  }, {
    "country": "Canada",
    "visits": 441
  }, {
    "country": "Brazil",
    "visits": 395
  }],
  "graphs": [{
    "fillAlphas": 0.9,
    "lineAlpha": 0.2,
    "type": "column",
    "valueField": "visits"
  }],
  "categoryField": "country"
}, 100);

let counter = 0;
const listener = () => {
  console.log('dataUpdated called', counter += 1);
};

chart.addListener('dataUpdated', listener);

let timer = setInterval(() => {
  chart.validateData(); //this triggers dataUpdated
  if (counter == 3) {
    chart.removeListener(chart, 'dataUpdated', listener);
    console.log('dataUpdated removed')
    chart.validateData(); //confirm that listner isn't called anymore
    clearInterval(timer);
  }

}, 1500);
html,
body {
  width: 100%;
  height: 100%;
  margin: 0px;
}

#chartdiv {
  width: 100%;
  height: 100%;
}
<script src="//www.amcharts.com/lib/3/amcharts.js"></script>
<script src="//www.amcharts.com/lib/3/serial.js"></script>
<script src="//www.amcharts.com/lib/3/themes/light.js"></script>

<div id="chartdiv"></div>

答案 1 :(得分:0)

只需使用像这样的监听器即可:

const listener = () => {
    console.log('Chart rendered! This should only print once!');
    // ...
    chart.removeListener(chart, 'rendered', listener);
};

chart.addListener('rendered', listener);

Here是显示行为的代码笔。


或者,您可以使用外部变量来保存状态:

let executedOnce = false;
chart.addListener('rendered', () => {
    if (executedOnce) {
      return;
    }
    executedOnce = true;
    console.log('Chart rendered! This should only print once!');
    // ...
});

此外,我建议切换到,因为它更加灵活并且甚至具有新的图表类型。您只需一张图表即可轻松迁移。您可以同时使用amcharts3和amcharts4(Migration Guide)。

在amcharts4中,您可以订阅一次事件(docs):

chart.events.once('datavalidated' => {
    // ...
});