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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:39:37+00:00 2026-05-25T13:39:37+00:00

To my way of thinking, I might be over-engineering with the recursive solution. Wait

  • 0

To my way of thinking, I might be over-engineering with the recursive solution.

Wait 2 seconds for first set of modules to be loaded:

function loadContents() {
  $('[data-content]:not(.loaded)').each( function() {
    $(this).load($(this).data('content'));
    $(this).addClass('loaded');
  });
}
loadContents();
setInterval(loadContents, 2000);

When all of first set of modules are loaded, check for new modules:

function loadContents() {
  var notLoaded = $('[data-content]:not(.loaded)'),
  notLoadedLength = notLoaded.length;
  notLoaded.each( function(i,element) {
    $(element).addClass('loaded');
    $(element).load($(element).data('content'), function() {
      // one more has been loaded
      notLoadedLength--;
      if (notLoadedLength == 0) {
        alert("countdown good");
        loadContents();
      }
    });
  });
}
loadContents();
  • 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-25T13:39:38+00:00Added an answer on May 25, 2026 at 1:39 pm

    You should be able to do all of this with success handlers and no polling with timers.

    You don’t specify exactly what you want to do, but if you want to load multiple things in parallel and know when they are all loaded, then you can just keep some sort of state on how many have been loaded and when the count shows that they are all now loaded, you will know you’re done.

    If you want to load them sequentially, then you can just load the next one from each success handler. It’s probably easiest to create a list of things to be loaded and just have a generic success handler that gets the next one in the list and kicks off it’s load and removes it from the list. When the list of remaining items to load is empty, you’re done.

    Edit: Looking further at your code, it looks like you’re loading them all in parallel. You can just create a success handler for each one that is loading, add the loaded class in that success handler and see how many more have not yet finished. I would suggest this:

    function loadContents() {
      $('[data-content]:not(.loaded)').each( function() {
        var obj = $(this);  // save in local variable in function closure so we can reference it in the success handler
        obj.load(obj.data('content'), function() {
            obj.addClass('loaded');
            if ($('[data-content]:not(.loaded)').length == 0) {
                // all pieces of content are now loaded
            } else {
                // some pieces of content are still loading
            }
        });
      });
    }
    loadContents();
    

    Edit 2: OK, based on your comments, I now understand the problem better. I would scope loadContents to a parent of the DOM tree and then call it on the newly loaded content from the success handler. This will work for inifinite levels and it’s safe because it only ever calls itself once for any given parent of the DOM tree. When there is no new content to load, it just has nothing to do and thus doesn’t call itself any more. Here’s what I would recommend:

    function loadContents(root) {
      $('[data-content]:not(.loaded)', root).each( function() {
        var obj = $(this);  // save in local variable in function closure so we can reference it in the success handler
        obj.load(obj.data('content'), function() {
            obj.addClass('loaded');
            loadContents(obj);  // now load any contents from the newly loaded content
        });
      });
    }
    loadContents(document.body);   // start it off by looking in the whole DOM tree
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to make a function (the ones I'm thinking of are
I'm curious How do you people think ? (I mean a way of thinking)
So I was thinking one way that you can bring method chaining into PHP
Coming from a Java background, this is the way I'm thinking: The server provides
Coming from a Java background, this is the way I'm thinking: The server provides
Just thinking about the best way to build an Order form that would (from
I'm thinking if there is a way to use javascript to pick up ANY
I have been thinking about the optimal way to create an XML file using
I have been thinking about a neat way of load balancing and one thing
I got to thinking today: what is the best way of getting a distinct

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.