我目前正在使用setInterval每10秒调用一次API请求,
但是,不是更新值,而是将值添加到页面
我认为这是因为我在调用值时将代码设计为+ =。
我不知道该用什么......
在工作中看到它 https://plnkr.co/edit/BEdYpVhJGCXyTPnAP482?p=preview
// Code goes here
var time = setInterval(function(){
const url = "https://api.kraken.com/0/public/Ticker?pair=xbtusd"; // Change
this
to your URL
fetch(url,{ mode: "cors"})
.then(function(response) {
if(response.status == 200) { // Check if response went through
response.json().then(function(data) {
console.log(data);
var price_USD = document.getElementById('price-usd');
var USDPrice = '<p>BTCUSD Price:' +
((data.result.XXBTZUSD.c[0])*1.1).toFixed(2) + '$' + '</p>';
price_USD.innerHTML += USDPrice;
});
} else { // Response wasn't ok. Check dev tools
console.log("response failed?");
console.log(response);
}
});
const urleur = "https://api.kraken.com/0/public/Ticker?pair=xbteur"; // Change
this to your URL
fetch(urleur,{ mode: "cors"})
.then(function(response) {
if(response.status == 200) { // Check if response went through
response.json().then(function(data) {
console.log(data);
var price_USD = document.getElementById('price-usd');
var USDPrice = '<p>BTCEUR Price:' +
((data.result.XXBTZEUR.c[0])*1.1).toFixed(2) + '€' + '</p>';
price_USD.innerHTML += USDPrice;
});
} else { // Response wasn't ok. Check dev tools
console.log("response failed?");
console.log(response);
}
});
},10000);
答案 0 :(得分:1)
我看到2个错误。
price_USD.innerHTML += USDPrice;
您可以price_USD.innerHTML = USDPrice;
和price_EUR.innerHTML = EURPrice;
答案 1 :(得分:0)
好的,我将+ =改为just =
我划分了块,而不是两块价格都有1块div,每个价格现在都有自己的div