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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:50:49+00:00 2026-05-24T20:50:49+00:00

To speed up my application I want to prepare some data before DOM is

  • 0

To speed up my application I want to prepare some data before DOM is ready and then use this data when DOM is ready.

Here’s how it might be:

var data = function prepareData(){
  ...
}();

$(document).ready(function() {

   // use data to build page

}

How to prepare the data for later use?
Thanks

  • 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-24T20:50:49+00:00Added an answer on May 24, 2026 at 8:50 pm

    You need should use parentheses around the function expression for clarity (and because in a similar situation where you’re defining and calling a function but not using the return value, it would be a syntax error without them). Also, when you use a function expression, you want to not give it a name. So:

    var data = (function(){
        ...
    })();
    

    or use a function declaration instead:

    var data = processData();
    function processData() {
        ...
    }
    

    (Why not use a name with a function expression? Because of bugs in various implementations, especially Internet Explorer prior to IE9, which will create two completely unrelated functions.)

    However, it’s not clear to me what you’re trying to achieve. When the browser reaches the script element, it hands off to the JavaScript interpreter and waits for it to finish before continuing building the DOM (because your script might use document.write to add to the HTML token stream). You can use the async or defer attributes to promise the browser you’re not going to use document.write, on browsers that support them, but…


    Update: Below you’ve said:

    because prepareData is long time function and I assumed that browser can execute this while it’s building DOM tree. Unfortunately ‘$(document).ready’ fires before prepareData is finished. The question is how to teach ‘$(document).ready’ to wait for ready data

    The only way the ready handler can possibly trigger while processData is running is if processData is using asynchronous ajax (or a couple of edge conditions around alert, confirm, and the like, but I assume you’re not doing that). And if it were, you couldn’t be returning the result as a return value from the function (though you could return an object that you continued to update as the result of ajax callbacks). Otherwise, it’s impossible: JavaScript on browsers is single-threaded, the ready handler will queue waiting for the interpreter to finish its previous task (processData).

    If processData isn’t doing anything asynchronous, I suspect whatever the symptom is that you’re seeing making you think the ready handler is firing during processData has a different cause.

    But in the case of asynchronous stuff, three options:

    1. If you’re not in control of the ready handlers you want to hold up, you might look at jQuery’s holdReady feature. Call $.holdReady(true); to hold up the event, and use $.holdReady(false); to stop holding it up.

    2. It’s simple enough to reschedule the ready handler. Here’s how I’d do it (note that I’ve wrapped everything in a scoping function so these things aren’t globals):

      (function() {
          var data = processData();
      
          $(onPageReady);
      
          function processData() {
          }
      
          function onPageReady() {
              if (!data.ready) {
                  // Wait for it to be ready
                  setTimeout(onPageReady, 0); // 0 = As soon as possible, you may want a
                                              // longer delay depending on what `processData`
                                              // is waiting for
                  return;
              }
          }
      
      })();
      

      Note that I happily use data in the onPageReady function, because I know that it’s there; that function will not run until processData has returned. But I’m assuming processData is returning an object that is slowly being filled in via ajax calls, so I’ve used a ready flag on the object that will get set when all the data is ready.

    3. If you can change processData, there’s a better solution: Have processData trigger the ready handler when it’s done. Here’s the code for when processData is done with what it needs to do:

      $(onPageReady);
      

      That works because if the DOM isn’t ready yet, that just schedules the call. If the DOM is already ready, jQuery will call your function immediately. This prevents the messy looping above.

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

Sidebar

Related Questions

Recently I use data binding to speed up my development of C# winforms application.
I have this code here, however i want to limit the speed the user
I've been working on a website lately and want to speed up my application.
I`m developing an application which receives data from my GPS device like coordinates, speed
Which data-access model has the best performance and speed for a C# Winforms application?
For my application I want to calculate my car/bike speed using iphone. How can
To speed up the unit tests I want to use SQLite instead of MySQL,
I want to use custom fonts in my j2me application. so I created a
I want to create a class to speed up things like getting application delegate,
I want write an win-form application to convert data from foxpro table into sql

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.