views:

32

answers:

5
+1  Q: 

jquery callback

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 ?

A: 

The following execute once the document has been loaded:

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

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

function SomethingElse()
{
}
hunter
A: 

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

lakhlaniprashant.blogspot.com
A: 

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

Fincha
A: 

http://api.jquery.com/queue/

Chouchenos
A: 

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/

AutoSponge