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 7779115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:34:09+00:00 2026-06-01T18:34:09+00:00

I have a script that takes 18 seconds to complete because it’s populating about

  • 0

I have a script that takes 18 seconds to complete because it’s populating 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 /* 300 or more */; ++ 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');

        // Set HTML content on front & back of module
        $modFront.html(contentFront);
        $modBack.html(contentBack);
    }
});

If I comment out the two .html() calls, run time drops to about 110ms, so those are clearly the culprits. And the HTML content is not a ton, maybe 300 bytes of data at most.

Any advice?

EDIT: Added code that creates the DIVs. Perhaps I could move that within the getJSON callback and just drop in the HTML as they’re created? Would that help?

  • 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-06-01T18:34:11+00:00Added an answer on June 1, 2026 at 6:34 pm

    I created module as a template and replaced the content with the response. And Moved your html logic below the JSON so it can done once. See below,

     var modTmpl = '<div class="module"><div><div class="face front">{CONTENT_FRONT}</div><div class="face back">{CONTENT_BACK}</div></div></div>';
    
    $.getJSON('html_server.php?callback=?', function(data) {
        var htmlCount = data.length;
    
        var pick, tmp, $mods = [];
        for (i = 0; i < numModules /* 300 or more */; ++ i)
        {
            pick = Math.floor(Math.random() * Math.floor(htmlCount/2)) * 2;
            tmp = modTmpl
                    .replace(/{CONTENT_FRONT}/, data[pick])
                    .replace(/{CONTENT_BACK}/, data[pick+1]);
            $mods.push(tmp);
        }
    });
    
    /*
        I am leaving the below code untouched as it seems like you are 
        positioning each module. If not then you can just 
        $('#wall').append($mods.join('')); and remove below for loop
    */
    for (i = 0; i < numModules-1; ++ i)
    {
        var $mod = $mods[i]; //use the pushed modules and append now
    
        modLeft += modSize;
        if (modLeft + modSize > $('#wall').width())
        {
            modLeft = 0;
            modTop += modSize;
        }
        $mod.css({
            left: modLeft,
            top: modTop
        }).appendTo('#wall');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a perl cgi script that's fairly resource intensive (takes about 2 seconds
I have the following code print Starting stage 1<br> # Something that takes about
I have a Perl script that takes about 30 minutes to run, so of
I have a script that takes a while. I'd like to display a page
I have a Perl script that takes text values from a MySQL table and
So basically I have a PHP script that takes all the images from the
I have to create a script that takes a mySQL table, and exports it
I have this small PHP script that takes a POST variable and writes it
I have a script that uses Parallel::ForkManager . However, the wait_all_children() process takes incredibly
I have a nant script that ... 1. takes the content of disc-file 2.

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.