2

I am loading data of HTML table dynamically from Jquery.

$(document).ready(function () {

    for (var i = 0; i < StudentsList.length; i++) {
        LoadRow(StudentsList[i]);
    }

});
function LoadRow(student) {
        setTimeout(function (student) {
         $("#tbl tbody").append("<tr class='trStyleSummary'><td>" + student.Name + "</td></tr>");
         }, 1000);
}

I want the table to load rows one by one with delay.

How do I do that? I tried SetTimeout with 1sec, But for some reason, its not working. The table is loading entirely after 1 second.

4
  • What problems did you have with set time out? Commented May 4, 2014 at 18:08
  • @Ian:Updated the post with answer for your comment. Commented May 4, 2014 at 18:09
  • how are you calling LoadRow, is there only one row. Share a bit more code ;) Commented May 4, 2014 at 18:13
  • @DurgeshChaudhary:updated Commented May 4, 2014 at 18:16

4 Answers 4

3

Working Fiddle

jQuery

$("tbody tr").each(function () {
   $(this).fadeIn($(this).index() * offset);
});  

Link to the result

Hope this is what you are looking for...!!

Sign up to request clarification or add additional context in comments.

Comments

1

Your code is not working because append is getting called at n:1000 for each row. Try following code, it will solve your problem, but it certainly is not a best approach.

$(document).ready(function () {

    for (var i = 0; i < StudentsList.length; i++) {
        LoadRow(StudentsList[i],i);
    }

});
function LoadRow(student,n) {
        setTimeout(function (student) {
         $("#tbl tbody").append("<tr class='trStyleSummary'><td>" + student.Name + "</td></tr>");
         }, 1000*n);
}

4 Comments

It says student.Name is undefinded. Any idea?
console.log(student) will reveal if that student really has a Name property
Inside the LoadRow() i am able to get the name. But inside settimeout function , the name is undefined
It is because of scope. Solution and explanation here : stackoverflow.com/questions/2171602/…
0

SetTimeout just runs things once. What you want is SetInterval so it runs like a clock. This is how I would solve it:

// From your example before
function LoadRow(student) {
  $("#tbl tbody").append("<tr class='trStyleSummary'><td>" + student.Name + "</td></tr>");
}    

// Say for example you have these as your students: 
var students = [{ Name: 'joe' }, { Name: 'matt' }, { Name: 'sherry' }];

// The index of the last student
var l = students.length;

// Start a Count index
var i = 0;

// START!
var t = setInterval(function(){

  // Load one
  LoadRow( students[i] );

  // Increment up
  i++;

  // Check if all is done, if so stop and clear out
  if (i == l) clearInterval(t);

}, 1000);

Comments

0

Should try this way

$(document).ready(function () {
    LoadRow(StudentsList);        
});

function LoadRow(list) {
    if(list != undefined || list.length < 1) {
        // done
        // do something else
        return;
    }

    // Get first item
    const student = list.shift();

    $("#tbl tbody").append(`<tr class='trStyleSummary'>td>${student.Name}</td></tr>`);
    setTimeout(function (student) {
        // Recall the function after 0.15 second
        LoadRow(list);
     }, 150);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.