简体   繁体   English

函数中的javascript存储设置变量

[英]javascript store set variable from within function

I have a file data.json with the contents: {"id": "foo", "value": "bar"} . 我有一个文件data.json ,内容为: {"id": "foo", "value": "bar"} I have another file index.html in the same directory with the following contents: 我在同一目录中还有另一个文件index.html ,内容如下:

<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
  var datavar;
  d3.json("data.json", function(d) { datavar = d; console.log(datavar); });
  console.log(datavar);
</script>

When I open index.html in a web browser, the first console.log(datavar) call outputs {id: "foo", value: "bar"} . 当我在Web浏览器中打开index.html时,第一个console.log(datavar)调用输出{id: "foo", value: "bar"} The second one outputs undefined . 第二个输出undefined I was under the impression that since I initialized datavar outside of the function, changes I make to it would last once we're back out of the function. 我的印象是,由于我在函数外部初始化了datavar ,因此一旦我们退出该函数,对它所做的更改将持续。 How can I store data from within the function that lasts outside the function? 我如何从功能外部存储功能中的数据?

You are correct in your assumption that a variable defined at the global level, and then changed within a function will persist the change outside of said function. 您的假设是正确的,即在全局级别定义的变量然后在函数中进行更改将使更改保持在所述函数之外。

However, this is outputting 'undefined' because it is an asynchronous operation. 但是,由于它是异步操作,因此输出为“ undefined”。 This means that the code changing 'datavar' may not have executed before you log it. 这意味着更改“ datavar”的代码在登录之前可能尚未执行。 If you add a note to your console logs, ie console.log('inside d3', datavar) and console.log('outside d3', datavar) you may be surprised to see the 'outside d3' execute first. 如果在控制台日志中添加注释,即console.log('inside d3', datavar)console.log('outside d3', datavar)您可能会惊讶地发现'outside d3'首先执行。

Async loading is most definitely what you want - so you should tailor your actual code accordingly. 绝对是您想要的异步加载-因此,您应该相应地调整实际代码。 Please feel free to provide more details if you run into issues. 如果遇到问题,请随时提供更多详细信息。

If you absolutely need to load the data synchronously, see this SO answer: How do I load JSON data synchronously with d3.js? 如果您绝对需要同步加载数据,请参见以下SO答案: 如何与d3.js同步加载JSON数据?

Your assumption that because the variable is initialized outside of the function it will be stored longer than the function is correct. 您的假设是,由于变量是在函数外部初始化的,因此存储的时间长于函数的正确时间。 What is incorrect is the order in which they get hit. 错误的是它们被击中的顺序。 d3.json calls an asynchronous function, which lets the rest of the application keep running while loading. d3.json调用一个异步函数,该函数使应用程序的其余部分在加载时保持运行。 So the console.log after the d3.json call happens (undefined), and then at some later time the data.json is loaded and the correct info is logged. 因此,发生d3.json调用后的console.log(未定义),然后在以后的某个时间加载data.json并记录正确的信息。

If you want to use this data you need to wait for it to return and only use it in a callback function. 如果要使用此数据,则需要等待其返回并仅在回调函数中使用它。 The way you are currently setting the variable will work, but you'd have to tell any code using it to re-run so it runs after the data from the file is loaded. 当前设置变量的方式可以使用,但是您必须告诉使用它的任何代码重新运行,以便它在加载文件中的数据之后运行。

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

相关问题 Javascript在另一个函数中设置变量? - Javascript set variable from within another function? 匿名Javascript函数还是存储在变量中? - Anonymous Javascript function or store within a variable? 变量未从函数内部设置 - Variable not getting set from within function 从 javascript 函数中更新 Razor 变量 - Update Razor variable from within a javascript function Javascript:从函数内部测试变量是否为数组 - Javascript: From within a function, test if variable is array or not 无法从函数Javascript中更新变量 - Cannot update variable from within a function Javascript 从递归Javascript函数中返回变量 - Return a variable from within a recursive Javascript function 将函数结果设置为外部javascript文件上同一函数内的变量 - Set function result to variable within same function on external javascript file 如何在视图中将模型变量设置为$(“ form”)。submit函数内部javascript中的值 - How to set a model variable in a view to a value from within javascript inside a $(“form”).submit function 设置一些变量后,是否可以存储并执行javascript函数调用? - Is it possible to store a javascript function call and execute it when some variable is set?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM