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

标签 javascript jquery json function highcharts

关于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/

相关文章:

javascript - 如何使用 Javascript 在 pdf 上打印表格?

javascript - 从头开始构建的 super 菜单 - Jquery 调整大小

javascript - 模拟按住 Ctrl 键

sql - SQuirreL SQL 客户端不显示 Json 数据

javascript - react : Grabbing properties from child components - how?

javascript - 使用 vis.js 的分层布局

ruby-on-rails - 如何将 javascript 值作为 json 对象传递给 ruby​​ on rails 中的 Controller ?

具有自定义字段表示的 json django 模型

javascript - 来自 Tiled 的 Tilemap 未在 Phaser 中渲染

javascript - jQuery 发送并显示每行文本区域的 ajax 结果