【发布时间】:2017-12-15 13:18:49
【问题描述】:
这是游戏的链接 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>";
【问题讨论】:
-
localStorage 仅在客户端可用,因此用户无法获得其他人的排名。您需要从服务器中提取数据,以便人们查看彼此的排名。
-
那我怎么能只为自己的分数做呢?
标签: javascript get set local store