简体   繁体   English

将事件监听器添加到数组索引

[英]Add Event Listener to Array index

I have an array that is being filled on page load by D3. 我有一个数组,由D3在页面加载时填充。 I want to access that same array at another time but I still need to confirm that it has been loaded. 我想在另一个时间访问相同的数组,但是我仍然需要确认它已被加载。

I don't know the propper way to do this so I just guessed a few time and I kept getting "not a function" errors 我不知道执行此操作的正确方法,所以我只猜了几次,就不断收到“不是函数”错误

First Code (populate my svg, snapshot of current values) 第一个代码(填充我的svg,当前值的快照)

d3.tsv("file.txt").then( function(data) { Loaded_Data[1]=data; document.getElementById("valve1").innerHTML = data[data.length-1]['OnOff']; //etc 
 });

much later, but still in a relevant timescale of async, I want to do some d3 graphing with this data, but I want to make sure that there is data in my array. 很久以后,但仍处于异步的相关时间范围内,我想对这些数据进行一些d3绘图,但我想确保数组中有数据。

Later Code (populate graphs, weeks of data) 以后的代码(填充图形,数周的数据)

Loaded_Data[1].addEventListener('load', () => {console.log("success"); //d3.graphing; });

I basically want a: while (array[1] is undefined){ Listen; 我基本上想要一个:while(array [1] is undefined){ } when done => graph; }完成后=>图形;

Or would it just be easier to load the entire set of documents again in another d3.tsv().then()? 还是将整个文档集再次加载到另一个d3.tsv()。then()会更容易? it seems like a waste of resources to reload the entire data. 重新加载整个数据似乎浪费资源。 What makes this difficult is the number of sources I have to load in, and consolidating my data into one array will be (hopefully) more convenient. 造成这一困难的是我必须加载的源数量,将数据整合到一个阵列中(希望)更加方便。

just graph in the then call after you read the data: 读取数据后,只需在then调用中绘制图形即可:

  d3.tsv("file.txt")
    .then(data => graph(data)); //data is available to be used at this point

also, if I'm reading this correctly, you are listening for the array to be populated with the load event, but that is only fired when the whole page is loaded and isn't relevant to the populating of a data structure. 同样,如果我正确地阅读了此内容,则说明您正在侦听要使用load事件填充的数组,但这仅在加载整个页面并且与填充数据结构无关时才触发。 If you need the data stored somewhere in addition to graphing, you could modify the above to: 如果除了制图之外还需要将数据存储在其他位置,则可以将以上内容修改为:

  d3.tsv("file.txt")
    .then(data => {
      graph(data);
      myArray = data;
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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