问题描述
有没有办法让 d3.csv() 同步而不是异步?
Is there any way to make d3.csv() be synchronous instead of asynchronous?
因为我根据页面上的不同触发器加载了多个 csv 文件,所以我的代码越来越混乱.
My code is getting to be a mess with callbacks since I load multiple csv files according to different triggers on the page.
推荐答案
d3.csv 在设计上是异步的,以防止页面冻结,因此在不更改 d3 库本身的情况下无法更改.
d3.csv is asynchronous by design to prevent pages from freezing up, so that can't be changed without changing the d3 library itself.
但是,您可以通过 d3.text() 预加载所有文件并调用 d3.csv.parse 或 d3.csv.parseRows,鉴于文本文件已加载,这将是同步的.
However, you could pre-load all your files via d3.text() and call d3.csv.parse or d3.csv.parseRows, which will be synchronous given that the text file has been loaded.
举个例子,参见 Mike Bostock 在这篇文章中的回答 这个帖子.
For an example, see Mike Bostock's answer in this post this post.
这篇关于d3:使 d3.csv 函数同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!