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

156
Views
How can I prevent my Ajax/Json function to load slower and slower?

index.php:

    <div class="start">START</div>
    <div class="content"></div>


    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="script.js"></script>

update1.php:

     <div class="fire">FIRE<div>
     <div class="result"></div>

update2.php:

$json = array(
        'unique' => uniqid(),
    );
echo json_encode($json);

script.js:

$(document).off("click", ".start").on("click", ".start", function (event) {

             $.ajax({
                url: "update1.php",
                data: {},
                type: "POST",
                success: function (data) {
                    $(".content").html(data);
                }
            })
});


$(document).off("click", ".fire").on("click", ".fire", function (event) {
        $.ajax({
            url: "update2.php",
            type: "POST",
            dataType: "json",
            encode: true,
            data: {},
            success: function (data) {
                $(".result").html(data.unique);
            }
        });
    });  

I load some data via Ajax and after that I load a Unique ID with json encode. It is working well. The only problem is, that on every click "FIRE" the loading of the Unique ID is getting slower and slower.

The loading time is more or less like this

  • Click "FIRE" ---> Loading time of Unique ID: 0sek
  • Click "FIRE" ---> Loading time of Unique ID: 2sek
  • Click "FIRE" ---> Loading time of Unique ID: 4sek
  • Click "FIRE" ---> Loading time of Unique ID: really long that the mouse waiting symbol is turning and turning.

...

I cannot find out, why this is happening.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The issue is with the PHP implementation of uniqid()

To avoid time-based collisions it will sleep for a given number of mircoseconds.

This can be mitigated against without losing the uniqueness that would occur in instances of time-based collision.

See detailed explaination here: Why is uniqid slow?

The key part of the solution is to set more_entropy to true to avoid time-based collisions.

By setting more_entropy to true, we choose to provide an additional source of entropy by appending data using php_combined_lcg() (a pseudo-random number generator), thus preventing time-induced collisions without having to sleep.

over 4 years ago · Santiago Trujillo 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!