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

213
Views
How to create incremental counter across multiple browser windows?

I've tried to use localStorage to sync a counter across multiple windows and I've found that since localStorage doesn't have a lock it is possible to have racing issue.

const root = document.getElementById('root');
let thisCounter = 0;
const interval = setInterval(() => {
  const counter = parseInt(localStorage.getItem('counter') || 0, 10);
  localStorage.setItem('counter', `${counter + 1}`);
  ++thisCounter;
  root.innerHTML = `global: ${counter + 1}, this: ${thisCounter}`;
  if (thisCounter >= 500) {
    clearInterval(interval);
  }
}, 100);

And when I open three Chrome browser windows (having separated threads) it appears that the counter would eventually run to 1498 or 1497 instead of 1500.

enter image description here

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

0

Separate the read and write operations.

You can use a JSON array structure to designate individual variables for each browser window for write operations. When you need to read, you can access all the individual variables and add them together.

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!