在Javascript回调函数中返回返回值

时间:2016-07-13 08:19:58

标签: javascript asynchronous javascript-objects

我遇到了返回回调函数值的问题。我有一个带有两个函数的主函数。

问题是主函数返回" undefined"。对我来说,从主函数中获取值非常重要。

var rate = 0;


function main(sum, from, to) {


  var script = document.createElement('script');
  script.setAttribute('src', "http://query.yahooapis.com/v1/public/yql?q=select%20rate%2Cname%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes%3Fs%3D" + from + to + "%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json&callback=parseExchangeRate");
  document.body.appendChild(script);


  function parseExchangeRate(data) {
    rate = parseFloat(data.query.results.row.rate, 10);
  }

  return rate * sum;
}

0 个答案:

没有答案