gpt4 book ai didi

javascript - 在函数外部使用 $when...then() 内部的变量

转载 作者:行者123 更新时间:2023-11-28 03:40:11 25 4
gpt4 key购买 nike

关于Use variable from one forEach Method in another的后续行动。如何在函数外部使用在 then() 中获得的值?

var first, second;
$.when(
$.getJSON(json1, result => first = result);
$.getJSON(json2, result => second = result);
).then(function() {
if (first && second) {
let results = first.map((item, index) => item.x / second[index].v);

}
});

我想在另一个函数中使用此函数之外的结果

我想在 highcharts 中使用这些结果值。

有办法做到这一点吗?

最佳答案

在回调函数中初始化Highchart.js。

var first, second;
$.when(
$.getJSON(json1, result => first = result);
$.getJSON(json2, result => second = result);
).then(function() {
if (first && second) {
let results = first.map((item, index) => item.x / second[index].v);

Highcharts.chart('container', {
...
});
}
});

关于javascript - 在函数外部使用 $when...then() 内部的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57351660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com