为什么在html中获取后变量没有改变?

时间:2021-01-30 15:58:55

标签: javascript html json

所以...代码是这样的:

<script>
var total_cases = 0;
var new_cases = 0;
var total_deaths = 0;

fetch('https://covid.ourworldindata.org/data/owid-covid-data.json')
.then(res => {
return res.json()
})
.then (raw_data => {

for (const key in raw_data) {
const country = raw_data[key];
const country_data = country.data;
const latest_data = country_data[country_data.length - 1];
console.log(latest_data);

if (country.location != "World") {
if (latest_data.total_cases != null) {
total_cases += latest_data.total_cases;
}
if (latest_data.new_cases != null) {
new_cases += latest_data.new_cases;
}
if (latest_data.total_deaths != null) {
total_deaths += latest_data.total_deaths;
}
}

}
});

document.getElementById("total_cases").innerHTML = total_cases;
document.getElementById("new_cases").innerHTML = new_cases;
document.getElementById("total_deaths").innerHTML = total_deaths;
</script>

'total_cases' 和另一个变量应该大于 0,但是结果显示,例如:

<div id="total_cases"></div>

结果是0。请帮忙我不知道为什么它显示为0。

0 个答案:

没有答案