return value from getJSON function

后端 未结 4 1332

I have a function with jquery getJSON and i need the return the result value back (to Use it somewhere else)

Here is the code:

function getval(){
jQuery.         


        
4条回答
  •  孤城傲影
    2021-01-24 18:05

    You might try using a callback function:

    function getval( callback ){
        jQuery.getJSON('http://data.mtgox.com/api/1/BTCUSD/ticker?callback=?', 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!' ) } );
        });
    
    });
    

提交回复
热议问题