我有一些通过fetch()接收到的信息,这些信息保持调用时的状态。原始的json每10秒更新一次,我想利用这一点,每10秒更新一次fetch。我该怎么做?
我的代码是:
fetch('https://api.nomics.com/v1/currencies/ticker?key=my_key&ids=BTC,ETH,XRP&interval=1d,30d&convert=USD&per-page=100&page=1')
.then(function (response) { return response.json(); })
.then(function (data) { appendData(data); });
function appendData(data) {
document.getElementById('pikcs').src = data[2].logo_url;
var mainContainer = document.getElementById("ideir");
for (var i = 0; i < data.length; i++) {
var div = document.createElement("div");
div.innerHTML = data[i].id + " " + data[i].name + "<br> $" + data[i].price; div.style.paddingRight = "50px";
var imzs = document.createElement("img");
imzs.src = data[i].logo_url; imzs.style.height = "40px";
mainContainer.appendChild(imzs);
mainContainer.appendChild(div);
}发布于 2021-03-21 18:53:02
您可以使用setInterval()
const interval = setInterval(function() {
fetch('https://api.nomics.com/v1/currencies/ticker?key=my_key&ids=BTC,ETH,XRP&interval=1d,30d&convert=USD&per-page=100&page=1')
.then(function (response) { return response.json(); })
.then(function (data) { appendData(data); });
}, 1000); //1000 means 1 sec, 5000 means 5 secondshttps://stackoverflow.com/questions/66731400
复制相似问题