从getJSON函数返回值 [英] return value from getJSON function
本文介绍了从getJSON函数返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有jquery getJSON的函数,我需要返回结果值(在其他地方使用它)
I have a function with jquery getJSON and i need the return the result value back (to Use it somewhere else)
这是代码:
function getval(){
jQuery.getJSON('http://data.mtgox.com/api/1/BTCUSD/ticker?callback=?', function(data) {
// We can't use .return because return is a JavaScript keyword.
return data['return'].avg.value;
});
}
$(function () {
$(document).ready(function() {
alert (getval());
});
});
这不起作用:(
我知道我可以从getJSON函数中调用外部函数,其值如下:
jQuery.getJSON('http://data.mtgox.com/api/1/BTCUSD/ticker?callback=?', function(data) {
// We can't use return because return is a JavaScript keyword.
mysecondfunction(data['return'].avg.value);
});
function mysecondfunction(value){
//use the value
}
但我必须从另一个调用 json 函数函数,因为json返回一个动态值,我需要使用它。
But i have to call the json function from another function because json return a dynamic value and i need to use it.
我希望它清楚......
I hope it clear...
非常感谢!!
推荐答案
以下是最终解决方案:
function getval( callback ){
jQuery.getJSON('http://data.mtgox.com/api/1/BTCUSD/ticker', function(data) {
// We can't use .return because return is a JavaScript keyword.
callback(data['return'].avg.value);
});
}
$(function () {
$(document).ready(function() {
getval( function ( value ) {
alert( 'Do something with ' + value + ' here!' );
} );
});
});
谢谢大家的帮助!!
这篇关于从getJSON函数返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文