I assume that some background is needed to understand this issue.
Each time you add a new row to a table on your web page, your script changes the model of your document in your browsers memory. The API you use for this is called the DOM (Document Object Model).
An individual DOM update does not always mean that the page will be redrawn. The decision depends on the browser when this set of DOM changes causes the browser to redisplay the page.
I donβt really know, but I assume that browsers usually update views, perhaps somewhere around 40-60 times per second. The fact is that if your screen printing takes less time than 1/60 of a second, you will not see partial updates.
By adding a timer, you can of course slow down the process of creating a table. As you understand, this will allow you to see the page redrawn at different points during the for loop.
If you want to force the browser to redraw the user interface, there are some JavaScript hacks that you could try applying. For example, one such hack is documented in Ajaxian: forced redrawing of the user interface from JavaScript .
If you intentionally want to run your program slowly and slowly add lines, say, one line per second, and while using some other things with JavaScript at the same time, you need to start the loop either by starting new iterations in some way or using another (JavaScript ) flow.
Schematic example of using setTimeout
to slow down printing (prints 1 line per second, just change the second parameter in window.setTimeout
to change the delay):
var table = $('<table id="table1"/>').appendTo($('div')); var i = 0; var counter = 1000; function addRow() { table.append('<tr><td>'+i+'-1</td><td>'+i+'-2</td><td>'+i+'-3</td></tr>'); i++; if(i<counter) { window.setTimeout( addRow, 1000 ); } } addRow();