我正在从通过应用程序的REST API检索到的json数据中加载表内容,这一切都发生在名为makeTable()的函数中,该函数通过每个查询的Query .append() 来填充空tbody预存的表。到这里一切都很好,没有问题。
然后,我不得不再次遍历该表,并进行更多的ajax调用,以根据其内容更新每个表行中的一个单元格。这需要一段时间,所以我希望表能够呈现,然后在第二批数据进入时进行更新。我在一个名为updateTable()的函数中进行此操作,该函数使用JQuery遍历表,从而更新了相关TD中的数据——效果很好。
所以:
$(document).ready(function(){
makeTable();
updateTable();
});
我对.append的理解是它应该会立即更新,但实际是直到updateTable中的所有代码完成后才显示该表。
我希望的(crappy伪代码)是:
When DOM is ready {
call makeTable function
display the table for the user
call updateTable function
}
希望我解释清楚了问题,也多谢你的帮助!
Call updateTable in a setTimeout(..., 0)
setTimeout(function () {
updateTable();
}, 0);
Try using callback functions in ajax onSuccess functions.
So when makeTable function will be really done display the table for user and when that is done update table using timeout as @Fox32 said.
backbone.js would be really good for this kind of stuff.