在本地存储中设置变量

Lew*_*ron 5 javascript html5 local-storage 2d-games

目前正在设计游戏并且想法是高分,因此当当前得分超过本地存储时,它将被替换:

localStorage.setItem('highScore', highScore);
var HighScore = localStorage.getItem('highScore');
if (HighScore == null || HighScore == "null") {
  HighScore = 0;
}

if (user.points > HighScore) {
  highScore = parseInt(HighScore);
}
return highScore 
Run Code Online (Sandbox Code Playgroud)

多谢你们

Ant*_*sma 11

这应该指向正确的方向.

// Get Item from LocalStorage or highScore === 0
var highScore = localStorage.getItem('highScore') || 0;

// If the user has more points than the currently stored high score then
if (user.points > highScore) {
  // Set the high score to the users' current points
  highScore = parseInt(user.points);
  // Store the high score
  localStorage.setItem('highScore', highScore);
}

// Return the high score
return highScore;
Run Code Online (Sandbox Code Playgroud)