一种选择是使用回调,如下所示:
function load_graphs(arg1, arg2, arg3, arg4, done) {
// then do stuff and when that stuff is done
// call your callback (in this case I named it "done")
done();
}
然后在你的方法调用中
load_graphs('national','','container-natl-rates','container-natl-counts', function() {
// first load_graphs() is complete
load_graphs('division','Western','container-west-rates','container-west-counts', function() {
// second load_graphs() is complete
load_graphs('division','Eatern','container-east-rates','container-east-counts', function() {
// third load_graphs() is complete
})
})
})
还有其他更高级的选项,例如使用第三方工具,例如async https://github.com/caolan/async or co https://github.com/tj/co
希望这可以帮助