javascript - 如何停止本地存储覆盖
问题描述
我不太了解本地存储,所以我希望我能正确解释这一点。在没有进入我的程序上下文的情况下,我基本上希望每次运行程序时都设置两个键值,“teamname”和“totalpoints”,但是每次运行程序时,这些值都会被覆盖到最近一次他们被设置了。我在某个地方看到我需要合并一个数组来解决这个问题,但是我真的不知道该怎么做。
function store()
{
localStorage.setItem("key value",[teamname, totalpoints]);
document.write(localStorage.getItem("key value"));
}
解决方案
如果我理解您的问题,您只需要某种方式来检查用户的状态,如果他们已经访问过,请不要覆盖他们的团队名称或分数:
let teamname
let totalpoints
if(localStorage.getItem("visited") !== "true"){
teamname = /* your code here */
totalpoints = /* your code here */
localStorage.setItem("teamname", teamname)
localStorage.setItem("totalpoints", totalpoints)
localStorage.setItem("visited", true)
}else{
/* do something else */
}
推荐阅读
- entity-framework-core - DbContext 没有连接字符串
- reactjs - NextJS 动态导入 SSR 组件
- sql - 多个连接 - 性能
- java - 如何在for循环内创建多个线程
- python - 为什么会出现这个错误 ValueError: Unrecognized marker style array?
- python - 将如何实现,在 3d 数组(Matlab/Python)中整理出所有非 3d 封闭元素
- apache-spark - 在 Spark 中构建的数据管道中,Apache 箭头的常见用例是什么
- python - 如何摆脱用户 discord.py 的所有角色
- python - 按字符串的第一个单词排序,这是一个数字
- java - 在 Thymeleaf/Spring Boot webapp 中切换语言后,月份未本地化为 EN