0

D3 provides a method .data() to acquire data from a selection. My simple experience suggests that applying this method may cost time, if not substantial, for the data to be loaded to a local variable using say, for instance, var data = d3.select('.someClass').data(), especially if the dataset is large.

I am wondering is if there is a mechanism to verify if the data has been acquired. This appears to be different from, for example an AJAX call, since there is no server concerned here. (Correct me if I am wrong.) One possible solution is to use setTimeout or use setInterval. However, they both require a pre-set time interval, which may cause unnecessary delay.

Is there a mechanism to trigger an 'alert' when the data acquisition using this .data() method has completed?

X...
  • 173
  • 1
  • 2
  • 12
  • This should be amongst the least of your concerns. The data stored on some DOM node will be handed down as a reference and then assigned to your local variable, which will not require any data acquisition. Most parts of your other code will be more time-consuming then this. – altocumulus Jul 10 '17 at 11:11
  • You are perhaps right. However, when I apply a `setTimout` or `setInterval` function around `var data = d3.select().data()` statement, it does take some time before the local variable is populated. On the other hand, if I don't 'wait' for this, this local variable is empty. This was why I asked this question. – X... Jul 10 '17 at 11:21
  • 1
    Welcome to the deeper workings of JavaScript ;-) That is the expected behaviour. For an in-depth explanation have a look at this [answer](https://stackoverflow.com/a/4575011/4235784) (not the accepted one) to [*"Why is setTimeout(fn, 0) sometimes useful?"*](/q/779379). Another great resource is available at [*How JavaScript Timers Work*](https://johnresig.com/blog/how-javascript-timers-work/). – altocumulus Jul 10 '17 at 11:52

0 Answers0