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

138
Views
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 answers
Answer question

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 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!