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
You
needshould 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:or use a function declaration instead:
(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
scriptelement, it hands off to the JavaScript interpreter and waits for it to finish before continuing building the DOM (because your script might usedocument.writeto add to the HTML token stream). You can use theasyncordeferattributes to promise the browser you’re not going to usedocument.write, on browsers that support them, but…Update: Below you’ve said:
The only way the
readyhandler can possibly trigger whileprocessDatais running is ifprocessDatais using asynchronous ajax (or a couple of edge conditions aroundalert,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, thereadyhandler will queue waiting for the interpreter to finish its previous task (processData).If
processDataisn’t doing anything asynchronous, I suspect whatever the symptom is that you’re seeing making you think thereadyhandler is firing duringprocessDatahas a different cause.But in the case of asynchronous stuff, three options:
If you’re not in control of the ready handlers you want to hold up, you might look at jQuery’s
holdReadyfeature. Call$.holdReady(true);to hold up the event, and use$.holdReady(false);to stop holding it up.It’s simple enough to reschedule the
readyhandler. Here’s how I’d do it (note that I’ve wrapped everything in a scoping function so these things aren’t globals):Note that I happily use
datain theonPageReadyfunction, because I know that it’s there; that function will not run untilprocessDatahas returned. But I’m assumingprocessDatais returning an object that is slowly being filled in via ajax calls, so I’ve used areadyflag on the object that will get set when all the data is ready.If you can change
processData, there’s a better solution: HaveprocessDatatrigger the ready handler when it’s done. Here’s the code for whenprocessDatais done with what it needs to do: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.