2017-12-15 13 views
0

에서 높은 점수를 전송하고 검색 게임에 대한 링크 당신이 난 그냥 어떤 기능을 알 필요가 그렇게 많이 나는 작업있어 한, 가입 및 재생하려면 로그인해야 https://jsfiddle.net/ab4gaf15/5/ 플레이어가 게임에서 얻은 점수는 로컬 저장 장치로 전송 된 다음 로컬 저장 장치에서 모든 플레이어의 점수를 보유한 테이블로 검색됩니다.로컬 스토리지 여기

/* This function is called when a logged in user 
    plays the game and gets a score */ 
function updateScore(newScore) { 
    //Get the JavaScript object that holds the data for the logged in user 
    var usrObj = JSON.parse(localStorage[localStorage.loggedInUser]); 

    //Update the user object with the new top score 
    /* NOTE YOU NEED TO CHANGE THIS CODE TO CHECK TO SEE IF THE NEW SCORE 
     IS GREATER THAN THE OLD SCORE */ 
    usrObj.topscore = newScore; 

    //Put the user data back into local storage. 
    localStorage[localStorage.loggedInUser] = JSON.stringify(usrObj); 
} 


/* Loads the rankings table. 
    This function should be called when the page containing the rankings table loads */ 
function showRankingsTable() { 
    //Get a reference to the div that will hold the rankings table. 
    var rankingDiv = document.getElementById("RankingsTable"); 

    //Create a variable that will hold the HTML for the rankings table 
    var htmlStr = ""; 

    //Add a heading 
    htmlStr += "<h1>Rankings Table</h1>"; 

    //Add the table tag 
    htmlStr += "<table>"; 

    //Work through all of the keys in local storage 
    for (var key in localStorage) { 
     //All of the keys should point to user data except loggedInUser 
     if (key !== "loggedInUser") { 
      //Extract object containing user data 

      //Extract user name and top score 
      htmlStr += "David"; 
      //Add a table row to the HTML string. 
     } 
    } 

    //Finish off the table 
    htmlStr += "</table>"; 
+0

localstorage는 클라이언트 측에서만 사용할 수 있으므로 다른 사람들의 순위는 사용자가 사용할 수 없습니다. 사람들이 서로의 순위를 볼 수 있도록 서버에서 데이터를 가져와야합니다. – michael

+0

내 점수만으로 어떻게 할 수 있습니까? –

답변

0

당신은 실제로 당신이

이는 로컬 스토리지에 값을 설정하는 예입니다 귀하의 예제에 필요한 모든 조각 ...

localStorage[localStorage.loggedInUser] = JSON.stringify(usrObj); 

및이의 예입니다있어 해당 값을 다시 검색하는 중 ...

테이블의 현재 사용자 점수를 표시하려면 e this ...

/* Loads the rankings table. 
    This function should be called when the page containing the rankings table loads */ 
function showRankingsTable() { 
    //Get a reference to the div that will hold the rankings table. 
    var rankingDiv = document.getElementById("RankingsTable"); 

    //Create a variable that will hold the HTML for the rankings table 
    var htmlStr = ""; 

    //Add a heading 
    htmlStr += "<h1>Rankings Table</h1>"; 

    //Add the table tag 
    htmlStr += "<table>"; 

    //Work through all of the keys in local storage 
    if (typeof localStorage[localStorage.loggedInUser] !== 'undefined') { 

     usrObj = JSON.parse(localStorage[localStorage.loggedInUser]); 
     htmlStr += "<tr>"; 
      htmlStr += "<td>"; 
       htmlStr += usrObj; 
      htmlStr += "<td>"; 
       htmlStr += "<td>"; 
        htmlStr += localStorage.loggedInUser; 
       htmlStr += "<td>"; 
      htmlStr += "</td>"; 
     htmlStr += "</tr>"; 

    } 

    //Finish off the table 
    htmlStr += "</table>"; 
}