0

Is there a way to call a JavaScript function after the .ready() function has executed?

I think there's something funny going on in some DOM structuring in my code, and I want to try calling jquery.('reload') on a section. But I need to call it AFTER the $(document).ready(function(){}) is called.

To clarify a bit more, I'm using jQuery's masonry, and I'm having an issue with the rendering of elements. It comes with a method 'reload', and when called inside of .ready(), doesn't render correctly, but if done on say, a scroll event, it works fine.

6
  • To clarify, do you want this code to execute after all handlers for the .ready event have been run? Commented Jul 27, 2012 at 0:42
  • In what moment, specifically, do you want to call this function? And what are you trying to accomplish? Commented Jul 27, 2012 at 0:45
  • I believe so, my expectations are to trigger a function after everything in .ready() has executed. Unless it doesn't 'execute'. If I'm mistaking how it actually works, then forgive me, I assume once the javascript loads, .ready is triggered, executes, then is done with that file.js or <script></script> Commented Jul 27, 2012 at 0:45
  • I updated my OP to hopefully clear up my question. Commented Jul 27, 2012 at 0:47
  • How about triggering scroll event at the end of your ready function? Commented Jul 27, 2012 at 0:53

3 Answers 3

1

If you want your code to be executed right after .ready() is executed, you can use a setTimeout of 0 milliseconds.

$.ready(function(){

    setTimeout(myCoolFunction,0);
});

This example will execute myCoolFunction as soon as possible after .ready() has occurrred.

Sign up to request clarification or add additional context in comments.

Comments

0

What does it mean jquery.('reload')?? Maybe you can call it last in the $(document).ready(function(){})!

1 Comment

I wasn't clear enough, I meant jQuery's masonry plugin using the .('reload') method. My problem is that when I generate a series of 'boxes' using masonry in say a loop, it doesn't render them right, but when I generate them via scroll event, which I assume has nothing to do with .ready() they render correctly.
0

You could try the .load() method. It's fired when every resource is loaded. .ready() is as soon as the DOM is fully loaded.

EDIT: Just noticed, the .load() shorthand is deprecated, use $().bind("load", fn) instead

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.