appspot get currency values
by ostapische
JavaScript
var currencies = { "USD": {},"AUD": {},"GBP": {} };
var currencyValuesLength = Object.keys( currencies ).length * ( Object.keys( currencies ).length - 1 );
// when all done
function whenAllDone() {
$.each( currencies, function( currency_from, values ) {
$.each( currencies[ currency_from ], function( currency_to, value ) {
$( document.body ).append( $( "<div>" + currency_from + " -> " + currency_to + " = " + value + "</div>" ) );
} );
} );
}
// get values
function getValues() {
$.each( currencies, function( currency_from, values ) {
$.each( currencies, function( currency_to, value ) {
if ( currency_from != currency_to ) {
$.ajax( {
url: "https://currency-api.appspot.com/api/" + currency_from + "/" + currency_to + ".jsonp",
dataType: "jsonp",
data: {
amount: "1.00"
},
method: "POST",
success: function( response ) {
// console.log( response );
currencies[ currency_from ][ currency_to ] = response.rate;
var currentLength = 0;
$.each( currencies, function( currency, values_temp ) {
currentLength += Object.keys( values_temp ).length;
} );
if ( currentLength == currencyValuesLength ) {
whenAllDone();
}
}
} );
}
} );
} );
}
// onload
$( function() {
getValues();
} );