Javascript innerHTML将变量显示为0

Javascript innerHTML将变量显示为0,javascript,Javascript,我试图使用innerHTML显示一个变量,该变量在json文件中通过一个循环运行并添加到自身中,但当它与innerHTML一起添加时,它显示为0。我使用console.log来确保它以它应该的方式添加了数字,而且确实是这样,所以我不太确定问题出在哪里(对我来说很简单,我今天就开始使用Javascript)。 Console.log: 它在网页中的外观: 我的Javascript: window.onload = function() { //Creates variable count var

我试图使用innerHTML显示一个变量,该变量在json文件中通过一个循环运行并添加到自身中,但当它与innerHTML一起添加时,它显示为0。我使用console.log来确保它以它应该的方式添加了数字,而且确实是这样,所以我不太确定问题出在哪里(对我来说很简单,我今天就开始使用Javascript)。
Console.log:
它在网页中的外观:
我的Javascript:

window.onload = function() {
//Creates variable count
var count = 0;
//Runs through json file in a loop so the count can find the object amount
$.getJSON("/quake/quake.txt", function(json1) {
    $.each(json1, function(key, data) {         
        count = count + 1;
        count = count - (Number(data.mag) < 3.1);
        console.log(count);
    });
});
//Displays count in element with ID quakecount
document.getElementById("quakecount").innerHTML += "Magnitude 3.1+ Earthquakes Displayed: " + count;
window.onload=function(){
//创建变量计数
var计数=0;
//在循环中运行json文件,以便计数可以找到对象数量
$.getJSON(“/quake/quake.txt”),函数(json1){
$.each(json1,函数(键,数据){
计数=计数+1;
计数=计数-(数字(data.mag)<3.1);
控制台日志(计数);
});
});
//显示ID为quakecount的元素中的计数
document.getElementById(“地震计数”).innerHTML+=“震级3.1+显示地震:”+count;

}

$。getJSON
异步的,它不会等待文件加载,它会移动到下一行,因此每次你得到
0
,在这个函数中移动你的
innerHTML
,它会正常工作

window.onload = function() {
//Creates variable count
var count = 0;
//Runs through json file in a loop so the count can find the object amount
$.getJSON("/quake/quake.txt", function(json1) {
    $.each(json1, function(key, data) {         
        count = count + 1;
        count = count - (Number(data.mag) < 3.1);
        console.log(count);
    });
//Displays count in element with ID quakecount
document.getElementById("quakecount").innerHTML += "Magnitude 3.1+ Earthquakes Displayed: " + count;

});
}
window.onload=function(){
//创建变量计数
var计数=0;
//在循环中运行json文件,以便计数可以找到对象数量
$.getJSON(“/quake/quake.txt”),函数(json1){
$.each(json1,函数(键,数据){
计数=计数+1;
计数=计数-(数字(data.mag)<3.1);
控制台日志(计数);
});
//显示ID为quakecount的元素中的计数
document.getElementById(“地震计数”).innerHTML+=“震级3.1+显示地震:”+count;
});
}

$。getJSON
异步的,它不会等待文件加载,它会移动到下一行,因此每次您获得
0
,在这个函数中移动
innerHTML
,它会正常工作

window.onload = function() {
//Creates variable count
var count = 0;
//Runs through json file in a loop so the count can find the object amount
$.getJSON("/quake/quake.txt", function(json1) {
    $.each(json1, function(key, data) {         
        count = count + 1;
        count = count - (Number(data.mag) < 3.1);
        console.log(count);
    });
//Displays count in element with ID quakecount
document.getElementById("quakecount").innerHTML += "Magnitude 3.1+ Earthquakes Displayed: " + count;

});
}
window.onload=function(){
//创建变量计数
var计数=0;
//在循环中运行json文件,以便计数可以找到对象数量
$.getJSON(“/quake/quake.txt”),函数(json1){
$.each(json1,函数(键,数据){
计数=计数+1;
计数=计数-(数字(data.mag)<3.1);
控制台日志(计数);
});
//显示ID为quakecount的元素中的计数
document.getElementById(“地震计数”).innerHTML+=“震级3.1+显示地震:”+count;
});
}

谢谢,这个成功了。谢谢,这个成功了。