javascript 页面重新加载后如何保留变量的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16879985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:20:15  来源:igfitidea点击:

How to retain value of the variable after the page is reloaded?

javascript

提问by meks

I have function in my code, which suppose to gradually decrease the refreshment of the page from 50 sec to 10 if the button is pressed. Here it is:

我的代码中有函数,如果按下按钮,它假设页面的刷新时间从 50 秒逐渐减少到 10 秒。这里是:

  <div id="signin" onclick="pgeReload()"></div>

   ....

 <script type="text/javascript">
   var count=0;
   function pgeReload(){

     if(count <= 4){
       count++;
     }
     var times = count*10000;
     var pospond = 50000-count;
     setTimeout(function(){window.location.reload()}, pospond);
   }

 </script>

My problem is that the reload of the page always occurs after 50 seconds because (if I got it correctly) the variable countis always reassigned to 0 when the page reloads.

我的问题是页面的重新加载总是在 50 秒后发生,因为(如果我正确理解)该变量count在页面重新加载时总是重新分配为 0。

My Question is can I retain the value of count after it has been incremented even after the refresh of the page?

我的问题是,即使在页面刷新后,我是否可以保留计数值增加后的值?

thank you very much for everyone who can help me with that.

非常感谢所有可以帮助我的人。

the version with html5. But still, the problem is the initial value. The count is always 0

带有 html5 的版本。但是,问题仍然是初始值。计数始终为 0

<script type="text/javascript">
    var count=0;

     function pagereload(){

        if(typeof(Storage)!=="undefined")
        {
           count++;
       window.content.localStorage[key]=count;
           var tempTime = parseInt(window.content.localStorage[key])*1000;
           var pospond = 50000-tempTime;
           setTimeout(function(){window.location.reload()}, pospond);

        }
        else{
         setTimeout(function(){window.location.reload()}, 30000);
         }
   </script>

回答by gal007

You can use localstorageof HTML5.

您可以使用HTML5 的本地存储

window.content.localStorage[key]=value; //Saving

window.content.localStorage[key]; //Accessing

delete window.content.localStorage[key]; //Delete

Where KEY = the name of the variable you want to store, and VALUE is the string value you want to save. This only supports saving Strings, but if you need to save an object you can "stringify" the object with JSON.

其中 KEY = 要存储的变量的名称,而 VALUE 是要保存的字符串值。这仅支持保存字符串,但如果您需要保存对象,您可以使用 JSON 对对象进行“字符串化”。

So you can do something like:

因此,您可以执行以下操作:

<script type="text/javascript">

function pagereload(){

if(typeof(Storage)!=="undefined")
{
var count=(window.content.localStorage[key])? parseInt(window.content.localStorage[key]): 0;
count++;
window.content.localStorage[key] = count;

var tempTime = count*1000;
var pospond = 50000-tempTime;
setTimeout(function(){window.location.reload()}, pospond);

}
else{
setTimeout(function(){window.location.reload()}, 30000);
}
</script>

回答by WoooHaaaa

You'd better have a look at browser cookie.

你最好看看浏览器的cookie。

http://www.w3schools.com/js/js_cookies.asp

http://www.w3schools.com/js/js_cookies.asp

回答by jfriend00

You can store data persistently on the browser using either cookies or Local Storage.

您可以使用 cookie 或本地存储将数据持久存储在浏览器上。

Then, in future pages on the same domain, you can then use javascript to retrieve the prior value. Without using a technique like this to store data locally, each reload of the browser page starts completely fresh.

然后,在同一域的未来页面中,您可以使用 javascript 来检索先前的值。如果不使用这样的技术在本地存储数据,浏览器页面的每次重新加载都是全新的。

Your other option is to store the data on the server on behalf of the current user. When the value is changed in the client, you would make an ajax call to the server so the new value can be stored on the server. You can then either retrieve the value from the server with ajax in a future page or the server can place the current value into the page each time it is requested. You would need some method of identifying the current user (often a user login) so the server could retrieve the proper user's data.

您的另一个选择是代表当前用户将数据存储在服务器上。当客户端中的值发生更改时,您将对服务器进行 ajax 调用,以便新值可以存储在服务器上。然后,您可以在以后的页面中使用 ajax 从服务器检索值,或者服务器可以在每次请求时将当前值放入页面中。您需要某种方法来识别当前用户(通常是用户登录),以便服务器可以检索正确用户的数据。

回答by janos

Using localstore is a great idea, but if you're looking for something simple you could just put the count in the query string when you reload the page, for example:

使用 localstore 是一个好主意,但如果您正在寻找一些简单的东西,您可以在重新加载页面时将计数放入查询字符串中,例如:

window.location.href = url + '?count=' + count

You can check the count or whether it is initial load based on the variable:

您可以根据变量检查计数或是否为初始加载:

window.location.search

For example if you reloaded the page with ?count=3then the value of this variable will be ?count=3. You could also just put the count itself in the query string to simplify your parsing, like this:

例如,如果您使用重新加载页面,?count=3则此变量的值将为?count=3. 您也可以将计数本身放在查询字符串中以简化解析,如下所示:

window.location.href = url + '?' + count