Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 951601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:45:43+00:00 2026-05-15T23:45:43+00:00

As my last question was closed for being too vague – here it is

  • 0

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 });
            });
        });
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:45:44+00:00Added an answer on May 15, 2026 at 11:45 pm

    The loop where you set the background images is the real problem. Look at it: you’re looping through all the <li> elements that you just got finished building from the “grid”. Then, inside that loop — that is, for each <li> element — you go through the entire “grid” array and reset the background. Each node will end up being set to the exact same thing: the background corresponding to the last thing in the array over and over again to the exact same background.

    The way you build the HTML is also very inefficient. You should loop through the grid and build up a string array with an <li> element in each array slot. Actually, now that I think of it, you really should be doing the first and second loops at the same time.

    function loadMap() {
      var html = [], bg = ['grass', 'mud', 'sand'];
      for (var i = 0, len = gridArray.length; i < len; ++i) {
        html.push("<li class='node " + bg[gridArray[i]] + "'></li>");
      }
      $(gridContainer).html(html.join(''));
    }
    

    Now you’ll also need some CSS rules:

    li.grass { background-image: url(grass.jpg); }
    li.mud { background-image: url(mud.jpg); }
    li.sand { background-image: url(sand.gif); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my last question was closed because it wasn't clear, so I'll try again because
I recently asked a question that got shot down for being too strongly worded.
You may notice from my last question that a problem caused some more problems,
This is a followup to my last question . I now have a byte[]
This should be my last question on Jquery Sortable...for a while :) I have
Closed as exact duplicate of this question . I have an array/list of elements.
Due to a problem posting, my last question (duplicate of this) was closed. Background
The last question I asked concerned how to bin data by x co-ordinate. The
My last question was somewhat narrower than this one. I am interested to know
My last question was about getting the string representation of an object serialized to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.