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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:11:43+00:00 2026-05-31T20:11:43+00:00

Im calling a for loop inside a for loop and its not working, here’s

  • 0

Im calling a for loop inside a for loop and its not working, here’s the code :

function PopulateMonths() {
for (var m = 0; m < 12; m++) {
    var $month = $('.d-month').clone();
    $month.find('.d-header').text(m);
    $month = PopulateDays($month);
    $month.appendTo('#diary').show();
    $month = null;
}
}

function PopulateDays($month) {
for (var d = 0; d < 30; d++) {
    var $row = $('.d-row').clone();
    $row.find('.d-day').text(d);
    $row.appendTo($month).show();
    $row = null;
}
return $month;
}

If I call PopulateDays manually 12 times it works fine, as soon as I try to loop 12 times using PopulateMonths() the page crashes, CPU usage goes through the roof so im assuming a lot of work is going on.

What am I missing?

  • 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-31T20:11:44+00:00Added an answer on May 31, 2026 at 8:11 pm

    I’ve had to tackle a very similar issue before. It’s because when you are cloning the element, you are also cloning their classes etc. And then you insert the cloned item into the document.

    The next time you try to look for an element with that class, it will find two instances – the original one, and the one you cloned in the previous step. Both will be cloned again, and now you have 4 elements on the page. So you’re basically doubling the number of elements on each iteration, and doing it 12 times for days, and 30 times for months.

    2^12 = 4096
    2^30 = 1,073,741,824
    

    These are just rough estimates of how large it can grow. I haven’t done any further analysis to find the exact numbers, but basically the exponential growth is eating up the CPU and crashing your browser.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a for loop in Matlab; inside it I am calling a function
I have a for loop, inside which I'm calling a method to show the
When calling functions that always throw from a function returning a value, the compiler
I was using a BackgroundWorker to download some web sites by calling WebClient.DownloadString inside
I'm creating child processes in a for -loop. Inside the child process, I can
I have a nested while loop inside a foreach loop where I would like
Is it generally best to avoid calling to render partial in a loop situation...
my Ajax calls don't seem to be asynchronous when wrapped inside a .each loop,
Calling TextRenderer.MeasureText as follows: TextRenderer.MeasureText(myControl.Text, myControl.Font); and comparing the result to the size of
Calling image = Image.open(data) image.thumbnail((36,36), Image.NEAREST) will maintain the aspect ratio. But I need

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.