如何与d3.js同步加载JSON数据? [英] How do I load JSON data synchronously with d3.js?

查看:483
本文介绍了如何与d3.js同步加载JSON数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的网站首次初始化时,它会查询服务器以获取一些数据。在此数据恢复之前,我无法在页面上显示任何内容。使用d3.js,我可以使用d3.json()来获取我的数据,但是因为它是异步的,我需要把整个页面逻辑放在回调函数中。如何请求数据并等待数据恢复?

When my site first initializes, it queries a server to get back some data. I can't lay anything out on the page until this data gets back. With d3.js, I can use d3.json() to get my data, but because it's asynchronous, I need to put the entire page logic in the callback function. How do I request the data and wait for it to come back?

推荐答案

您基本上是唯一的方法。回调函数必须是启动代码其余部分的函数。你不需要所有的代码在回调函数,虽然,你可以引入间接。所以回调函数将调用另一个函数,其中将是您的回调函数中的当前。

You're basically doing it the only way. The callback function has to be the one initiating the rest of your code. You don't need all your code in the callback function though, you can introduce indirection. So the callback function will call another function inside which would be what is currently in your callback function.

这篇关于如何与d3.js同步加载JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆