Below is the code I use to build an HTML table on the fly (using JSON data received from the server).
I display an animated pleasewait (.gif) graphic while the data
You basically want to set up your loops so they yield to other threads every so often. Here is some example code from this article on the topic of running CPU intensive operations without freezing your UI:
function doSomething (progressFn [, additional arguments]) {
// Initialize a few things here...
(function () {
// Do a little bit of work here...
if (continuation condition) {
// Inform the application of the progress
progressFn(value, total);
// Process next chunk
setTimeout(arguments.callee, 0);
}
})();
}
As far as simplifying the production of HTML in your script, if you're using jQuery, you might give my Simple Templates plug-in a try. It tidies up the process by cutting down drastically on the number of concatenations you have to do. It performs pretty well, too after I recently did some refactoring that resulted in a pretty big speed increase. Here's an example (without doing all of the work for you!):
var t = eval('(' + request + ')') ;
var templates = {
tr : '#{row} ',
th : '#{header} ',
td : '#{cell} '
};
var table = '';
$.each(t.hdrs, function (key, val) {
table += $.tmpl(templates.th, {header: val});
});
...
- 热议问题