Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
Increment a counter every time the page refreshes

How to increment and save counter value on page refresh ?

<!DOCTYPE html>
<html>
<body onload="myFunction()">
<h1>Hello World!</h1>
<script>

var count;
var index;
function myFunction() {
count++;
alert("after count++"+count);
localStorage.setItem("incCount",count);


alert("index here :"+index);
if(index==1){
var s=localStorage.getItem("incCount");
alert("s is"+s)
}
index++;

localStorage.setItem("incIndex",index);
var g=localStorage.getItem("incIndex");
alert("g is "+g)


alert("index after index++"+index)

  alert("Page is loaded");
}
</script>

</body>
</html>
    

I want to increase the count variable value by 1 , every time I refresh the page .But , its not working as per my expectation , what should I do ?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Read the count from local storage, convert it to a number using the Number function, and increment it while storing.

<!DOCTYPE html>
<html>

<body onload="myFunction()">
  <h1>Hello World!</h1>

  <script>
    function myFunction() {
      let count = Number(localStorage.getItem('count')) || 0;
      alert(`count ${count}`);
      localStorage.setItem('count', count + 1);
    }
  </script>

</body>

</html>
about 4 years ago · Juan Pablo Isaza Report

0

The first thing you're doing on page load is increment an undefined variable count to 1, then storing that in localStorage incCount. So that will always be 1.

Instead, you need to read the existing value first and increment that. (Here I use || 0 to handle the case where no localStorage yet exists):

var count;
function myFunction() { // you should probably use a better name here
  const previousCount = Number(localStorage.getItem("incCount")) || 0;
  count = previousCount + 1;
  localStorage.setItem("incCount",count);
}

I've omitted the index variable here because I'm really not clear what you were trying to do with that, or if it's related to the given question; to just keep an incrementing count you only need the one variable.

about 4 years ago · Juan Pablo Isaza Report

0

To increment a count on page refresh all you have to do is to first check whether there is already a value for that incCount key or not.

If it is not then you have to create a localStorage key-value and if it is already there then you just have to set incremented value.

<!DOCTYPE html>
<html>

<body onload="myFunction()">
  <h1>Hello World!</h1>
  <script>
    let count = 0; // change

    function myFunction() {
      const valueInLS = localStorage.getItem("incCount");
      alert(valueInLS);
      if (valueInLS) count = valueInLS;
      localStorage.setItem("incCount", ++count);
    }
  </script>

</body>

</html>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!