2017-01-01 150 views
0

我试图创建一个保存在localStorage的高分。该应用程序是一个quizz应用程序,它可以保存并显示最后5个分数(其完成与否)。这里的问题是,我在html文件中创建的p标签不会被我在js文件中创建的高分列表中的值。这里是代码:p标签没有更新

<div id = "EndSection"> 
<h3>The Game is Over. Top 5 Players are:</h3> 
<div class = "Player"> 
<p id = "Player1">xoxo</p> 
<p id = "Score1">30</p> 
</div> 

,然后我刚才复制的DIV级选手与2 P标签,直到我有5名球员。

的JavaScript代码:

< function SaveToDataBase(){ 

//Checks if they completed the quizz, and check if there time is 
//enough to be top5, if its enough, then it will run newEntry 
var boolean = false; 
var Trigger = "score"; 

HighScoreList.forEach(function(entry){ 

    if(entry.hasOwnProperty(Trigger)){ 
    var value = entry[Trigger]; 
    /*you look in your scores, and his total time is less than theres, then he is 
    added to the list*/ 
    if(thetotaltimer < value){ 
     boolean = true; 
    } 
    } 
}); 
if(boolean){ 
    newEntry(); 
} 


} 


function newEntry(){ 
//inserts on index 
HighScoreList.splice(5, 0, {name: currentPlayer, score: thetotaltimer }); 

//sort scores 
HighScoreList.sort(function(a,b){ 
    return a.score - b.score; 
}); 
//starts at index 5, then takes away the index right after it (number 6). 
HighScoreList.splice(5,1); 
localStorage.setItem("SavedHighListObjectz", JSON.stringify(HighScoreList)); 

document.getElementById("Player1").innerText = JSON.parse(HighScoreList[0].name); 
document.getElementById("Score1").innerText = JSON.parse(HighScoreList[0].score); 
document.getElementById("Player2").innerText = JSON.parse(HighScoreList[1].name); 
document.getElementById("Score2").innerText = JSON.parse(HighScoreList[1].score); 
document.getElementById("Player3").innerText = JSON.parse(HighScoreList[2].name); 
document.getElementById("Score3").innerText = JSON.parse(HighScoreList[2].score); 
document.getElementById("Player4").innerText = JSON.parse(HighScoreList[3].name); 
document.getElementById("Score4").innerText = JSON.parse(HighScoreList[3].score); 
document.getElementById("Player5").innerText = JSON.parse(HighScoreList[4].name); 
document.getElementById("Score5").innerText = JSON.parse(HighScoreList[4].score); 
} 

function init(){ 

//this if-statement runs if there is no highscore list already saved 
if(localStorage.getItem("SavedHighListObjectz") == undefined) { 
HighScoreList = [ 
    {'name': "Sam", 'score': 300000}, 
    {'name': "Markus", 'score': 554000}, 
    {'name': "Cody", 'score': 2210000}, 
    {'name': "David", 'score': 39000000}, 
    {'name': "Jackson", 'score': 55500000} 
]; 
localStorage.setItem("SavedHighList", JSON.stringify(HighScoreList)); 

}else{ 
//or else we load this 
HighScoreList = JSON.parse(localStorage.getItem("SavedHighListObjectz")) 
    } 
    } 
init(); 
questions();> 

在此先感谢。

+0

您可以发布它的jsfiddle? 我觉得你的问题是在attritube空间的应该是这样的:'ID =“EndSection”的'instand:'ID =“EndSection”' – itzikb

+0

开始通过删除所有的解析代码。它是一个javaScript对象,因此不需要解析它。然后点击''''创建一个[mcve] – mplungjan

+2

我想要避免命名变量“布尔”。除其他原因之外,它没有描述它在做什么。 – seemly

回答

0

各种小毛病是从工作停止代码:

1. 您的姓名和分数只是字符串 - 试图JSON.parse他们应该导致“意外的标记”异常。你没看到吗?请确保您能够看到的JavaScript异常(在Chrome中,按F12键,然后单击“控制台”)

2. localStorage.setItem(“SavedHighList”应该是localStorage.setItem(“SavedHighListObjectz”。为了避免这样的错误,你的密钥保存为一个变量:

var storageKey="SavedHighListObjectz"; 
... 
localStorage.getItem(storageKey) 
... 

3. 你只得到了一个球员,这可能是显而易见的,但p标签(你已经固定后,#1和#2)的示例代码将在document.getElementById("Player2").innerText轰炸,直到您添加HTML的其他4个最高分

+1

在JavaScript自动类型转换之后,null = undefined是真的。使用全等测试中删除所有的疑问:'如果(localStorage.getItem(“SavedHighListObjectz”)===空)' – traktor53

+0

感谢@ Traktor53你是对的 - 已删除的项目符号 –