Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7042925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:13:36+00:00 2026-05-28T02:13:36+00:00

I have a strange problem and I wasn’t sure how to word the title.

  • 0

I have a strange problem and I wasn’t sure how to word the title.

What I’m Trying To Do:

I want to keep track of a running total and I want this running total to update live to my page every second. I’m not trying to track visitors, it’s going to track something weird like “amount of blood cells in your body right now!” Here is a website that does what I want to do, but they do it in jquery, I’m trying to do it in JS to keep the JS files to a minimum. http://www.usagain.com/ (left side)

How I’m Doing It:

I have a JS file with AJAX that is linked to a PHP file and that PHP file opens a Text file -> grabs a number -> increments it by 1 -> sends said number back to the JS -> Updates the number to HTML -> and the PHP updates the text file -> close txt file.

My Problem:

The counter works, it increments but the problem is if I have 2 browsers running the same page the number will increment by 2. If I have 3 browsers; the number will increment by 3 and so on. I think it has something to do with the writing to the file but I’m not sure how to fix it.

My Code

HTML/CSS/Javascript/AJAX

<html>
<head>
<title>Counter</title>
<script language="javascript" src="../jquery1.6.js"></script>
<script type="text/javascript">

    function addCommas(nStr) //http://www.mredkj.com/javascript/nfbasic.html -- Source
    {
        nStr += '';
        x = nStr.split('.');
        x1 = x[0];
        x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        return x1 + x2;
    }

    function getNum()
    {
        $.post('test.php', function(data){
            $('#counter').html(addCommas(data));
        })
    }   
    setTimeOut(getNum, 1000);
</script>
<style type="text/css">
    #counterContainer{color: #52504D;font-family:Verdana, Geneva, sans-serif;font-weight:bold;font-size:15px;position:relative;top:22px;}
    #counter{color: #1E7EC8; font-size: 25px;letter-spacing:1px;}
</style> 
</head>
<body onload="getNum()">
    <div id="counterContainer">
        <div id="counter"><!--Counter Goes Here, Do Not Disturb--></div>
    </div>
</body>
</html>

PHP File

<?php
    $fp = fopen("staticNum.txt", "r+");
    flock($fp, LOCK_EX);
    $num = fgets($fp, 11);
    $num = intval($num)+1;

    echo $num;

    fseek($fp, 0, SEEK_SET);
    fputs($fp, "$num");
    flock($fp, LOCK_UN);
    fclose($fp);
?>

My Text File just has this number in it:

10000100260

Any suggestions would be great. My first thought was a database but then I figured I’d have the same problem. I do want to stay away from Session variables and Cookies though for sure since I don’t think they’re necessary. I could be wrong though.

Bonus points if you can figure out a way to solve my problem without a database! (Not really though im not an admin 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:13:37+00:00Added an answer on May 28, 2026 at 2:13 am

    Instead of counting, try with timestamp:

    value = ( timestamp % ((max_limit - min_limit) / 1.5 ) ) * 1.5 + min_limit
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strange problem. I do this query in a node.js server using
we have strange problem, we have single signon and we are trying to fetch
I have a strange problem. When I want to create a new Spring MVC
I have a strange problem with select on chrome. I want to add a
I have a strange problem here, I want to create an emtpy trigger: SET
Good day! I have strange problem with Xcode 4.3. When I'm trying to run
I have a strange problem with opencv running on an Ubuntu. I installed OpenCV
i have strange problem doing reporting: i have numerous clients with different issued invoices.
I have strange problem with receiving data from socket. On client im using air
I have a strange problem on ipv6 connection. I write down a simple client

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.