Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda