如何自动增加本地存储的javascript项值?

如何自动增加本地存储的javascript项值?,javascript,local-storage,Javascript,Local Storage,我想为本地存储添加javascript,这样每次访问我的网站时,“firsttimer”项的值都会增加1。知道我该怎么做吗?提前感谢你 $(document).ready(function() { var isshow=localStorage.getItem('firsttimer'); if (isshow== null) {localStorage.setItem ('firsttimer', 1);} }); $(

我想为本地存储添加javascript,这样每次访问我的网站时,“firsttimer”项的值都会增加1。知道我该怎么做吗?提前感谢你

$(document).ready(function()
  { var isshow=localStorage.getItem('firsttimer');
 if (isshow== null) 
{localStorage.setItem ('firsttimer', 1);}
});

$(document).ready(function()
  { var isshow=localStorage.getItem('firsttimer');
 if (isshow== 1) 
{localStorage.setItem ('firsttimer', 2);}
});

...so on till infinty

您可以使用简单的
++
而不是硬核数字,如:

$(document).ready(function() {
  let isshow = localStorage.getItem('firsttimer');
  if (isshow === null) {
    localStorage.setItem('firsttimer', 1);
  }else{
    localStorage.setItem('firsttimer', ++isshow);
  }
});
显然,我不知道本地存储是如何组成的,或者假设它只是一个数字而不是一个数组:)



感谢@Jaromanda X提供有关增量前的建议

localStorage是浏览器的本地存储。。。那么,您想在浏览器中存储特定用户访问您网站的次数吗?另外,
getItem(&';firsttimer&';)
。。。我确信您的浏览器开发人员工具控制台中有错误。。。在阅读之后和编写之前,请尝试
isShow=isShow+1
。Jaromanda X是的,您希望在浏览器中存储特定用户访问您网站的次数,并根据他们的访问次数向他们显示不同的结果。例如,为第一次访问的人发送的欢迎消息等。感谢您的回复,但前提是isshow为null。如果是1、2或更多呢?我是javascript的新手,很抱歉打扰你。我修改了我的代码,如果是
null
总是
1
,因为这是第一次,否则请将
1
添加到
localstorage
谢谢,帮了我的忙!非常感谢,代码正在运行,您可以在这里查看:呃。。。你的意思是
++isshow
。。。由于当isshow==1时存储
isshow++
将存储1-因此,firsttimer只能是1(或者在首次访问之前不存在)