これはCoinMarketCap.com ticker APIに接続する単純なバニラJSスクリプトです。Xhr:API Cryptocurrency価格警告スクリプトが間違った値を返します
それはやるべきこと:
1)オブジェクト
2に格納し、コインのデータを取得する)この場合、(3分)
3時間のX量を待っ)、)1を繰り返して、より最近のデータ
4を取得するために)最近のデータと過去のデータ
5)の場合を比較 二つのオブジェクト(firstValue
とsecondValue
)が重複している:コインの価値は何であることはない
プロセスを繰り返し)、console.log
警告
6を上がってきました。また、やり直し(main()
関数を再度呼び出す)、これらの2つのオブジェクトの内容がアップに失敗し、以前の値が表示されます。
// Variables
// Set first recorded value
var firstValue;
// Set second value (most recent)
var secondValue;
// Get the obj from CoinMarketCap API
function getCoinValues() {
var requestURL ="https://api.coinmarketcap.com/v1/ticker/?limit=10";
var request = new XMLHttpRequest();
request.open("GET", requestURL);
request.send();
// When loaded assign to obj variable and return it
request.onload = function getUsdValues() {
// Save data to the 'obj' object
obj = JSON.parse(request.response);
console.log("Retrieved coin data");
};
return obj;
}
// Wait
function wait(ms){
console.log("Waiting.")
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}
// Compare two objects
function comparePrices (obj1, obj2) {
// Take price from one array, compare to the other
for (var i = 0; i < obj1.length; i++) {
// console.log(JSON.stringify(obj2[i].id) + " OLD:" + obj1[i].price_usd + "NEW:" + obj2[i].price_usd)
if (obj2[i].price_usd > obj1[i].price_usd) {
console.log(JSON.stringify(obj2[i].id) + " has increased!");
console.log("From $" + obj1[i].price_usd + " to $" + obj2[i].price_usd);
}
}
}
// Main function //
function main() {
// Get 1st coin values
console.log("Getting first values");
firstValue = getCoinValues();
// Wait
console.log("Waiting")
wait(30000);
// Retrieve new values
console.log("Getting second values")
secondValue = getCoinValues();
// Compare two sets of values
console.log("About to compare prices")
comparePrices(firstValue, secondValue);
console.log("Prices compared")
// Do it all again
// "Emptying" the variables
firstValue = null
secondValue = null
// Starting the main loop
main();
}
main();
はなぜfirstValue
とsecondValue
は価格がティッカーで効果的に多様持っている場合でも、異なる結果が表示されないのですか?
技術的専門用語の不適切な使用、および全体的なコードの初心者を許してください。
これは素晴らしいです!あなたの徹底、@アレックスありがとう!私は自分の(多くの)間違いをすべて試してみるためにあなたのコードを調べます。 – user2331291
受諾済み!ご協力いただきありがとうございます。 – user2331291