1

Trying to have a counter on a web page that does not restart on each different page view within the same user session. Currently using this code (thanks to Praveen Kumar Purushothaman) but this counter resets every time a different page is viewed.

setTimeout(start, 0);
var i = 0;
var num = document.getElementById("number");

function start() {
  increase();
  setInterval(increase, 1000);
}

function increase() {
  if (i < 100000) {
    i += 10.41;
    num.innerText = i.toFixed(2);
  }
}
<span id="number"></span>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Cherubrock74
  • 35
  • 1
  • 7

1 Answers1

0

My suggestion is storing the variable into session storage. I have added more details in the comments:

setTimeout(start, 0);
// You're saving your current value here.
// Let's use localStorage. Set the i value if it doesn't exist for the first time.
if (!localStorage.getItem("i")) {
  localStorage.setItem("i", 0);
}
var num = document.getElementById("number");

function start() {
  increase();
  setInterval(increase, 1000);
}

function increase() {
  var i = localStorage.getItem("i");
  if (i < 100000) {
    i += 10.41;
    // When you're making any changes, make changes to the localStorage too.
    localStorage.setItem("i", i);
    num.innerText = i.toFixed(2);
  }
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • can the counter restart from 0 on the next session from the same user? Right now if I leave the page at 2300 for example when i visit the page again another day it starts from 2300...is there a way to drop the session when you close the browser? please advise thank you – Cherubrock74 Nov 16 '20 at 17:36
  • @Cherubrock74 Session Storage does that. They have some time limit when they expire. I guess if the browser is closed and reopened, it goes away. – Praveen Kumar Purushothaman Nov 16 '20 at 17:53
  • For some reason in my browser (Firefox) for example the session does not seem to expire...counter still restarting from the last number seen before closing the browser and reopening...same for Edge browser...any way to force the session drop when closing the browser or navigating away from the web site? – Cherubrock74 Nov 17 '20 at 17:37
  • @Cherubrock74 Sure I'll find something that works across the browsers. – Praveen Kumar Purushothaman Nov 17 '20 at 17:39
  • any luck with this? please advise – Cherubrock74 Nov 23 '20 at 19:02
  • @Cherubrock74 Found something already. Forgot to share: https://stackoverflow.com/questions/39128931 – Praveen Kumar Purushothaman Nov 23 '20 at 19:18
  • it seems like I have to use sessionStorage instead of localStorage if i want the counter to keep going when user browse across the web pages and only when it closes the tab it will restart from 0...I tried the suggested script but I cant get it to work...considering the code above that I am using what would be the way to accomplish this? – Cherubrock74 Nov 30 '20 at 20:14
  • @Cherubrock74 Last attempt, try using Cookies with an expiry time? – Praveen Kumar Purushothaman Nov 30 '20 at 20:50
  • the example posted is regarding user being logged in and still uses localStorage...how would loginstatus=loggedin be used in my script? please advise thank you – Cherubrock74 Nov 30 '20 at 22:17