2017-01-01 153 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你是對的 - 已刪除的項目符號 –