jquery callback

2019-05-22 09:56发布

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 ?

4条回答
男人必须洒脱
2楼-- · 2019-05-22 10:41

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

查看更多
小情绪 Triste *
3楼-- · 2019-05-22 10:46

The following execute once the document has been loaded:

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

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

function SomethingElse()
{
}
查看更多
萌系小妹纸
4楼-- · 2019-05-22 10:50

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/

查看更多
beautiful°
5楼-- · 2019-05-22 10:50

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

查看更多
登录 后发表回答