我有这个代码显示比特币价格和基础和其他东西,但我想让它在数据到达之前(我将 setInterval 设置为 3 秒)它显示“等待数据到达...”并在之后它到达时显示“数据已到达!” (PS:我正在使用 Axios 库)这是我的 HTML 和 JS HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON Test</title>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<h1 id="num"></h1>
<h1 id="num1"></h1>
<h1 id="num2"></h1>
<h1 id="num3"></h1>
<h1 id="num4"></h1>
<h1 id="num5"></h1>
<script src="zoom.js"></script>
</body>
</html>
和JS
const num1 = document.getElementById("num1");
const num2 = document.getElementById("num2");
const num3 = document.getElementById("num3");
const num4 = document.getElementById("num4");
const num5 = document.getElementById("num5");
const num = document.getElementById("num");
if(num1,num2,num3,num4,num5.innerText.length > 0) {
num.innerText = `Data Arrived!`
} else {
num.innerText = `Waiting for Data to Arrive...`
}
setInterval(() => {
const bitcoin = axios.get('https://api.cryptonator.com/api/ticker/btc-usd')
.then(res => {
num1.innerText = `${"Current Base"}, ${res.data.ticker.base}`
num2.innerText = `${"Current Currency"}, ${res.data.ticker.target}`
num3.innerText = `${"Current Bitcoin Price"}, ${res.data.ticker.price}`
num4.innerText = `${"Current Volume"}, ${res.data.ticker.volume}`
num5.innerText = `${"Current Change"}, ${res.data.ticker.change}`
})
.catch(err => {
num1.innerHTML = `<strong>ERROR: Nothing found!<strong>`
num2.style.opacity = 0;
num3.style.opacity = 0;
num4.style.opacity = 0;
num5.style.opacity = 0;
})
}, 5000);
这是我尝试过的方法,还是我的 If 和 Else 语句的语法有问题?
if(num1,num2,num3,num4,num5.innerText.length > 0) {
num.innerText = `Data Arrived!`
} else {
num.innerText = `Waiting for Data to Arrive...`
}
撒科打诨
相关分类