Javascript-使用Yahoo Weather Api将函数返回为var
这是我的代码Javascript-使用Yahoo Weather Api将函数返回为var,javascript,api,yahoo,weather,Javascript,Api,Yahoo,Weather,这是我的代码 <script> var callbackFunction = function(data) { var temp = data.query.results.channel.item.condition.temp; console.log(temp); return temp; } var $temp = callbackFunction; </script> <script sr
<script>
var callbackFunction = function(data) {
var temp = data.query.results.channel.item.condition.temp;
console.log(temp);
return temp;
}
var $temp = callbackFunction;
</script>
<script src="https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20%3D%202487889&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys/&format=json&callback=callbackFunction"></script>
它不起作用。我也试过:
var callbackFunction = (function (data) {...}) ();
这对这个不起作用,但我用其他方法尝试了这个解决方案,它起了作用
谢谢大家! 我以前也尝试过声明它,然后在回调中定义它。它只返回“undefined”,您在哪里尝试获取此变量的值?它将仅在调用回调后填充,而不是立即填充。看到为什么不在回调函数中分配$temp了吗?@GiannisMp,我试过了,但在回调函数之外我不能使用它。@HereticMonkey,我在文件开头声明了变量,然后在各种函数中使用它们。我能做点什么吗?
var callbackFunction = (function (data) {...}) ();