Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda