在本地存储中设置变量 [英] setting a variable in local storage
本文介绍了在本地存储中设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前正在设计一款游戏,思路是高分,这样当当前的分数大于本地存储的时候就替换掉:
Currently designing a game and the idea is that of a high score, so that when the current score is more than the local storage one it is replaced:
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
谢谢各位
推荐答案
这应该会为您指明正确的方向.
This should point you in the correct direction.
// 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;
这篇关于在本地存储中设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文