I have a script that lasts 18 seconds because it fills in about 300 DIVs with HTML data. Code below:
HTML:
<div id="window">
<div id="wall">
<div class="module">
<div>
<div class="face front"></div>
<div class="face back"></div>
</div>
</div>
</div>
</div>
JavaScript:
for (i = 0; i < numModules-1; ++ i)
{
var $mod = $('.module:eq(0)').clone();
modLeft += modSize;
if (modLeft + modSize > $('#wall').width())
{
modLeft = 0;
modTop += modSize;
}
$mod.css({
left: modLeft,
top: modTop
}).appendTo('#wall');
}
$.getJSON('html_server.php?callback=?', function(data) {
var htmlCount = data.length;
for (i = 0; i < numModules ; ++ i)
{
var pick = Math.floor(Math.random() * Math.floor(htmlCount/2)) * 2;
var contentFront = data[pick];
var contentBack = data[pick+1];
var modStyle = '';
var $mod = $('.module:eq(' + i + ')');
var $modFront = $mod.find('.front');
var $modBack = $mod.find('.back');
$modFront.html(contentFront);
$modBack.html(contentBack);
}
});
If I comment on two calls .html(), the execution time will be reduced to about 110 ms, so they are clearly the culprits. And the HTML content is not a ton, and maybe not more than 300 bytes of data.
Any tips?
EDIT: Added code that creates a DIV. Perhaps I could move this in the getJSON callback and just drop the HTML as they were created? Will this help?
source
share