简体   繁体   English

刷新时清除本地存储值

[英]Local storage value clears on refresh

I have a function which draws a graph from the 7 values selected by the user.我有一个 function,它根据用户选择的 7 个值绘制图表。 It works fine, but the local storage values set for each of the variable dissapears on a refresh它工作正常,但是为每个变量设置的本地存储值在刷新时消失了

function drawChart() {
      

 var dates = document.getElementById("datepicker").value;
    var days;
    for(days=1;days<=dates;days++)
    {
        document.write(days);
    }



const coordinates2 = dates;
      
const[one,two,three,four,five,six,seven
 

]= coordinates2.split(',')




localStorage.setItem('one',one); // storing is successful but clears on refresh



// i have also tired Json.stringify but it did not good

localStorage.setItem('one', JSON.stringify(one)); //storing is successful but clears on refresh

}

I can see that the browser is storing the variable value, and it disappears as soon as the browser refreshes.我可以看到浏览器正在存储变量值,浏览器一刷新它就消失了。 Is there something else i need to do.还有什么我需要做的吗? Thanks a lot for your time.非常感谢您的宝贵时间。

I have a function which draws a graph from the 7 values selected by the user.我有一个 function 从用户选择的 7 个值中绘制图表。 It works fine, but the local storage values set for each of the variable dissapears on a refresh它工作正常,但为每个变量设置的本地存储值在刷新时消失

function drawChart() {
      

 var dates = document.getElementById("datepicker").value;
    var days;
    for(days=1;days<=dates;days++)
    {
        document.write(days);
    }



const coordinates2 = dates;
      
const[one,two,three,four,five,six,seven
 

]= coordinates2.split(',')




localStorage.setItem('one',one); // storing is successful but clears on refresh



// i have also tired Json.stringify but it did not good

localStorage.setItem('one', JSON.stringify(one)); //storing is successful but clears on refresh

}

I can see that the browser is storing the variable value, and it disappears as soon as the browser refreshes.我可以看到浏览器正在存储变量值,并且浏览器一刷新它就消失了。 Is there something else i need to do.还有什么我需要做的。 Thanks a lot for your time.非常感谢您的时间。

You need to check if the value is already set before you set it设置前需要检查该值是否已经设置

if (!localStorage.getItem('one')){
localStorage.setItem('one',one);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM