在本地存储中设置变量 [英] setting a variable in local storage

查看:216
本文介绍了在本地存储中设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆