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

151
Visualizações
Add the ability to render comments when the comment-list elements should be filled with text "Loading..." and show results one by one

I'm trying to render results one by one with some text like "Loading..." and show results one by one. I found answers at one, two and tried to solve this but it shows only one loading text and renders, shows all results at a time. The results should appear one after the other, with the order preserved. If any error occurs during the loading, the tag should be left empty. How to solve this challenge? For full question please refer here

HTML

<!DOCTYPE html>
<html>
    <head>
        <title>Render Comments</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    </head>
    <body>
        <div class="comment-list" data-count=1></div>
        <script type="text/javascript" src="./app.js"></script>
    </body>
</html>

JS

"use-strict";

var commentList = $(".comment-list");
var count = $(".comment-list").data("count");
var url = "https://jsonplaceholder.typicode.com/comments?postId=" + count;

$.ajax({
  url: url,
  method: "GET",
  beforeSend: function () {
    commentList.html("Loading...");
  },
  success: function (data) {
    commentList.empty();
    data.forEach(function (comment) {
      commentList.append(`
           <div class="comment-item">
              <div class="comment-item__username">${comment.id}</div>
              <div class="comment-item__message">${comment.name}</div>
          </div>`);
    });
  },
  error: function () {
    // commentList.empty();
    data.forEach(function () {
    commentList.append(`
         <div class="comment-item">
         <div>Oops! Error occured to show the results</div>
         </div>`);
    });
  },
});

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You might consider doing this with CSS instead of with javascript. There are many examples including link. If you insist on doing this with javascript you need to break up the code adding the elements to the page using setInterval or setTimeout.

about 4 years ago · Juan Pablo Isaza 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