2

I need to be able to have a callback on execution of a function after ready

jQuery(document).ready(function() {    
   execute function 1, only when finish
      do function 2
});

what is the good way to do that ?

5
  • and what is the problem? Commented Sep 28, 2010 at 13:31
  • What is the "something" you're trying, is it asynchronous? Commented Sep 28, 2010 at 13:32
  • i have no sure way to know the function 1 is finish before doing finction 2 Commented Sep 28, 2010 at 13:33
  • Are you talking about .queue() ? Commented Sep 28, 2010 at 13:35
  • What is function 1? In almost all cases, you know it's done just because the control came back to you and went to function 2. You mean it's asyncronous then? Commented Sep 28, 2010 at 13:36

4 Answers 4

2

The following execute once the document has been loaded:

$(function(){
    DoSomething();
});

function DoSomething()
{
    SomethingElse(); // depends what you're doing in DoSomething()
}

function SomethingElse()
{
}
Sign up to request clarification or add additional context in comments.

Comments

1

JavaScript is single-threaded. Nothing happens simultaneously. However, if fn1 is an async call, the thread will execute fn2 often before fn1's return value is ready. That's what makes JavaScript "racy."

If you have multiple aysnc calls in a series, then you either have to manage a series of NESTED callbacks or make a queue using the Active Object pattern. jQuery has one (mentioned). I wrote a library that does this called proto-q: http://code.google.com/p/proto-q/

Comments

0

yes this is a good way. you can use: (function(){}); if you are not using javascripts libs other than jQuery for ready

Comments

0

you can let function1 call a second function then the third one, and so on. but it is allways line by line

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.