rle*_*mon 14 javascript jquery multithreading jquery-ui progress-bar
所以我有一个大的JSON对象,我从服务器返回,然后从它构建一个数据表并在表单上显示它.这通常需要几秒钟......所以我想到了一个加载栏.我有加载栏背后的逻辑,但是构建hmtl数据的循环是锁定浏览器而我无法调用我需要更新的元素.
这是我的功能:
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(" ", " ") + "</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=\"0\"><thead><tr>" + headers + "</tr></thead><tbody>" + contents + "</tbody></table>";
$(container_id).html(html);
$(container_id).children("table:first").dataTable({
"bJQueryUI": true,
"sScrollX": "100%"
});
});
}
Run Code Online (Sandbox Code Playgroud)
mae*_*ics 20
[添加我的评论作为答案]
JavaScript是单线程的.你必须将你的工作分成几部分并使用"setTimeout"按顺序调用它们以允许GUI在处理期间(在你的调用之间)更新,但即使这样,浏览器仍然会显得有点无响应.
| 归档时间: |
|
| 查看次数: |
30300 次 |
| 最近记录: |