admin管理员组文章数量:1314442
I have a callback function that I'd like to execute after 2 separate $.getScript
calls have successfully loaded.
I'm using the below design, but it feels messy, and the use of a named function feels very un-jQuery
var foo_ready = false;
var bar_ready = false;
var dual_callback = function(){
if(foo_ready && bar_ready)
{
//do things
}
};
jQuery.getScript('/foo.js', function(){
foo_ready = true;
dual_callback();
});
jQuery.getScript('/bar.js', function(){
bar_ready = true;
dual_callback();
});
Is there a better or more elegant way to do execute a single callback after multiple asynchronous calls have pleted?
I have a callback function that I'd like to execute after 2 separate $.getScript
calls have successfully loaded.
I'm using the below design, but it feels messy, and the use of a named function feels very un-jQuery
var foo_ready = false;
var bar_ready = false;
var dual_callback = function(){
if(foo_ready && bar_ready)
{
//do things
}
};
jQuery.getScript('/foo.js', function(){
foo_ready = true;
dual_callback();
});
jQuery.getScript('/bar.js', function(){
bar_ready = true;
dual_callback();
});
Is there a better or more elegant way to do execute a single callback after multiple asynchronous calls have pleted?
Share Improve this question asked Mar 5, 2011 at 3:44 YahelYahel 37.3k23 gold badges106 silver badges154 bronze badges 01 Answer
Reset to default 12If you have jQuery 1.5.1 you can do this:
$.when( $.getScript('/foo.js'), $.getScript('/bar.js') ).done(function(){
// this is called when both are done.
});
Check out the Deferred Object and $.when()
本文标签: Dual or Multiple Callback Design Pattern with JavaScriptjQueryStack Overflow
版权声明:本文标题:Dual or Multiple Callback Design Pattern with JavaScriptjQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741970455a2407805.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论