如何避免回调链? [英] how to avoid callback chains?
本文介绍了如何避免回调链?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一堆函数以严格的顺序调用。
现在我使用链接的回调函数:
callMe1(function(){
callMe2(function(){
callMe3(function(){
callMeFinal );
});
});
});
这可行,但似乎有点丑陋。
$ b $
如果您使用jQuery,那么您可以使用 http://api.jquery.com/queue/> queue
链接函数。
$(document)
.queue(callMe1)
.queue(callMe2);
其中 callMeX
/ p>
function callMeX(next){
// do stuff
next
}
I need a bunch of functions to be called in strict order. It's also very important that the next function waits until the previous one has finished.
Right now I'm using chained callbacks:
callMe1(function(){
callMe2(function(){
callMe3(function(){
callMeFinal();
});
});
});
This works but seems to be a little ugly.
Any suggestions for a different approach?
解决方案
If you use jQuery, then you can use queue
to chain the functions.
$(document)
.queue(callMe1)
.queue(callMe2);
where callMeX
should be of form:
function callMeX(next) {
// do stuff
next();
}
这篇关于如何避免回调链?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文