Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  194] [ 7]  / answers: 1 / hits: 32500  / 13 Years ago, tue, august 9, 2011, 12:00:00

So I have a large JSON object i'm returning from the server, then building a datatable from it and displaying it on the form. This usually takes a few seconds.. so I was thinking of a loading bar.
I have the logic behind the loading bar, however the loop that builds the hmtl data is locking down the browser and I cannot call out to the element i need to update.



Here is my function to do this:



function buildDataTable(db_table, container_id) {
var $pb = $(<div id=progress-bar></div>);
$(container_id).html($pb);
$pb.progressbar({
value: 0
});
$.post(post location, {
view: all
}, function (data) {
var headers = ;
var contents = ;
var jsonObject = $.parseJSON(data);
var tik = Math.round(jsonObject.length / 100);
for (key in jsonObject[0]) {
headers += <th> + key.replace( , &nbsp;) + </th>;
}
for (i in jsonObject) {
contents += <tr>;
for (j in jsonObject[i]) {
contents += <td class=border-right> + jsonObject[i][j] + </td>;
}
contents += </tr>;
if(Math.round(i/tik) == i/tik) {
/* if I run the alert (between popups) i can see the progressbar update, otherwise I see no update, the progressbar appears empty then the $(container_id) element is updated with the table i've generated */
alert('');
$pb.progressbar(value,i/tik);
}
}
var html = <table cellpadding=5 cellspacing=><thead><tr> + headers + </tr></thead><tbody> + contents + </tbody></table>;

$(container_id).html(html);
$(container_id).children(table:first).dataTable({
bJQueryUI: true,
sScrollX: 100%
});
});
}

More From » jquery

 Answers
335

[Added my comment as an answer]



JavaScript is single threaded. You'll have to break your work up into pieces and call them in sequence using setTimeout to allow the GUI to update during processing (in between your calls) but even then the browser will still seem somewhat unresponsive.


[#90715] Monday, August 8, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gideons

Total Points: 197
Total Questions: 106
Total Answers: 108

Location: Moldova
Member since Sat, Jan 29, 2022
2 Years ago
;