2

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:

  <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 count is always reassigned to 0 when the page reloads.

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

<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>
4

4 回答 4

3

You can use localstorage of 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.

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>
于 2013-06-02T05:15:38.890 回答
1

You'd better have a look at browser cookie.

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

于 2013-06-02T05:19:03.107 回答
1

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

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.

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.

于 2013-06-02T05:20:59.980 回答
1

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

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

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

window.location.search

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

window.location.href = url + '?' + count
于 2013-06-02T06:24:22.043 回答