views:

78

answers:

2

As my last question was closed for being "too vague" - here it is again, with better wording.

I have a "grid" of li's that are loaded dynamically (through JavaScript/jQuery), the Array isn't huge but seems to take forever loading.

So, SO people - my question is:
Am I being stupid or is this code taking longer than it should to execute?

Live demo: http://jsfiddle.net/PrPvM/ (very slow, may appear to hang your browser)

Full code (download): http://www.mediafire.com/?xvd9tz07h2u644t

Snippet (from the actual array loop):

var gridContainer = $('#container');
    var gridArray = [ 
        2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,2,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,2,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    ];

    function loadMap() {
        var i = 0;
        while (i <= gridArray.length) {
            var gridHTML = $(gridContainer).html();
            $(gridContainer).html(gridHTML+'<li class="node"></li>');
            i++;
        }
        $('li.node').each(function() {
            $(gridArray).each(function (i, val) {
                if (val == '0') { gridTile = 'grass.jpg' };
                if (val == '1') { gridTile = 'mud.jpg' };
                if (val == '2') { gridTile = 'sand.gif' };
                $($('ul#container :nth-child('+i+')'))
                  .css({ 'background-image': 'url(img/tiles/'+gridTile });
            });
        });
    }
+9  A: 
Pointy
I don't suppose you would have any idea on how I could merge the two loops together?
Neurofluxation
Give me a sec and I'll type it in :-)
Pointy
Regarding the first paragraph: he won't use the same background for every node, because the selector used to set the background relies on `nth-child` to pick the correct one. The outer loop - the one looping over `li.node`s - serves no purpose other than to make this exponentially slower... Your code side-steps all of these problems (and more) - nice work.
Shog9
Ah - you're right; it *will* repeat the .css call for each one, but they'll get the right value. I'll fix my answer - thanks!!
Pointy
Omg, pure genius! Excellent work Pointy - That has sped up my code like crazy!
Neurofluxation
+3  A: 

It'd probably be farm more efficient to build up the complete HTML for the array and then assign it to the .html property of the container, rather than assigning each individual li:

    var gridHTML = $(gridContainer).html();
    while (i <= gridArray.length) {
       gridHTML = gridHTML+'<li class="node"></li>';
        i++;
    }
    $(gridContainer).html();

Next, why are you looping over both of these? The outer loop is probably completely unnecessary, because your inner loop already uses nth-child to select the proper node.

    $('li.node').each(function() {
        $(gridArray).each(function (i, val) {
Amber