I suggest two improvements:
- Cache
DOM References - Work at the table offline
Example
function prepareTable() {
var groupIndex = 0;
var $mytable = $('#row'),
$parent = $mytable.parent();
$mytable = $mytable.detach();
$mytable.find('tr').each(function(index) {
var $this = $(this);
var group = $this.attr('id', 'node-'+index).removeClass("odd event").find('td :hidden').attr('value');
if (group == 'true') {
groupIndex = index;
$this.addClass('odd').find("td:first")
.mouseenter(function() {
$this.parent().addClass("swGroupLink");
})
.mouseleave(function() {
$this.parent().removeClass("swGroupLink");
});
} else {
$this.addClass('even child-of-node-' + groupIndex);
}
});
$parent.append($mytable);
}
I have added a variable $thisthat caches $(this)in your loop .each(). I also added $mytableand $parent. $mytablestores the element #row, and $parentstores the parent-node from #row. This is because I remove the entire element from the DOM, do the work, and reattach it to the parent.
: http://www.jsfiddle.net/2C6fB/4/
, . -, , . , , asychronous , , setTimeout. , , setTimeout(). → http://www.jsfiddle.net/2C6fB/5/
, "" . , , , .