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>
Sourabh Bhagat
  • 1,691
  • 17
  • 20
meks
  • 777
  • 3
  • 12
  • 23

4 Answers4

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>
gal007
  • 6,911
  • 8
  • 47
  • 70
  • I'm not so good in html5. Besides not all the browsers support it. Do you know how can I do it in javascript? I mean any equvivalent? – meks Jun 02 '13 at 05:17
  • wait a sec I will try – meks Jun 02 '13 at 05:20
  • is this option supported by any browser? – meks Jun 02 '13 at 05:20
  • 1
    All modern browsers support HTML5, even in mobile. – gal007 Jun 02 '13 at 05:20
  • Web storage is supported in Internet Explorer 8+, Firefox, Opera, Chrome, and Safari. Only Internet Explorer 7 and earlier versions, do not support web storage. – gal007 Jun 02 '13 at 05:21
  • I see. how about those users who use earlier versions? – meks Jun 02 '13 at 05:22
  • is there any javascript equivalent for that option? – meks Jun 02 '13 at 05:22
  • I really think there are no too much old users today. You will always have to set aside a set of users. I think aside the old users is best, they are a few and are upgrading, and mobile using almost unaffected. – gal007 Jun 02 '13 at 05:25
  • There are alternatives depending on what are you developing. If you are developing a plugin you have fileSystem access. – gal007 Jun 02 '13 at 05:27
  • You can create cookies, but if user doesn't have it enabled you cant use it – gal007 Jun 02 '13 at 05:28
  • ok. let me try html 5 first – meks Jun 02 '13 at 05:30
  • there is still a problem. even if I use local storage the `count` value will be always 1 – meks Jun 02 '13 at 05:51
  • like this `window.content.localStorage[key]=count; retriveValue = window.content.localStorage[key] * 1000;` it will be always 1 :( – meks Jun 02 '13 at 05:52
  • No. Yoy need to convert the values returned. Remember you are storing STRINGS not numbers – gal007 Jun 02 '13 at 05:52
  • And if you want to change you need to do:window.content.localStorage[key]= parseInt(window.content.localStorage[key])*1000; – gal007 Jun 02 '13 at 05:55
  • ok I see. But still if I place it in `window.content.localStorage[key]=count;` in the code I will always pass 0 to the storage because on reload there is `var count = 0` – meks Jun 02 '13 at 05:58
  • So, don't do that in reload. Make var count = window.content.localStorage[key] – gal007 Jun 02 '13 at 05:59
  • but the purpose of that function is to reload the page gradually. if i din't do reload what is the purpose of the function. – meks Jun 02 '13 at 06:04
  • I tried this: `` – meks Jun 02 '13 at 06:04
  • I will better update the post – meks Jun 02 '13 at 06:04
  • I updated the post. have alook – meks Jun 02 '13 at 06:07
  • the problem is where to put `var count = 0` to avoid reassigning it to 0 – meks Jun 02 '13 at 06:08
  • is there is javascript something like `ifNotPostBack` loke in VB? – meks Jun 02 '13 at 06:08
  • the problem is god deamed `var count = 0` – meks Jun 02 '13 at 06:10
  • – gal007 Jun 02 '13 at 06:17
  • I updated my answer too – gal007 Jun 02 '13 at 06:20
  • can I use this: `if(<%=(Page.IsPostBack).ToString().ToLower()%>){count = 0}` – meks Jun 02 '13 at 07:09
  • or I think it will not work. I tried – meks Jun 02 '13 at 07:16
1

You'd better have a look at browser cookie.

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

WoooHaaaa
  • 19,732
  • 32
  • 90
  • 138
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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

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:

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=3 then 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:

window.location.href = url + '?' + count
janos
  • 120,954
  • 29
  • 226
  • 236
  • should I still place `count = 0` in the code? – meks Jun 02 '13 at 06:42
  • The code should get `count` from `window.location.search`. If it's not there, then use `count=0`, if it's there, then use that as the count. – janos Jun 02 '13 at 07:24