是否可以让Head JS的ready()函数等待两个脚本?

Jan*_*ard 10 javascript head.js

我在我的网页上加载了三个脚本,我想在其中两个脚本完成加载后触发一个函数.

 head.js(
     { webfont: 'http://ajax.googleapis.com/ajax/libs/webfont/1.0.31/webfont.js' },
     { jquery: 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js' },
     { analytics: 'http://www.google-analytics.com/ga.js' }
 );
Run Code Online (Sandbox Code Playgroud)

理想情况下,我希望能够执行以下操作,但根据文档(请参阅脚本组织),似乎无法使head.ready()等待加载两个脚本.

head.ready('jquery', function() {
    // Code that requires jQuery.
});

// This is not supported. :-(
head.ready('jquery', 'analytics', function() {
    // Code that requires both jQuery and Google Analytics.
    // ...
});
Run Code Online (Sandbox Code Playgroud)

那我该怎么解决呢?如果我嵌套就绪方法,我可以确定我的代码将被触发,还是只有在jquery在分析之前完成加载时才会触发它?

head.ready('jquery', function() {
   // Code that requires jQuery.
   // ...
   head.ready('analytics', function() {
       // Code that requires both jQuery and Google Analytics.
       // ...
   });
});
Run Code Online (Sandbox Code Playgroud)

另一种解决方案可能是将加载语句分成两部分,如下所示.但是,我仍然可以从异步加载脚本中获益,还是在jquery和分析之前完成加载webfont?

 head.js(
     { webfont: 'http://ajax.googleapis.com/ajax/libs/webfont/1.0.31/webfont.js' }
 );

 head.js(
     { jquery: 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js' },
     { analytics: 'http://www.google-analytics.com/ga.js' },
     function() {
         // Code that requires both jQuery and Google Analytics.
         // ...
     }
 );

 head.ready('jquery', function() {
     // Code that requires jQuery.
     // ...
 });
Run Code Online (Sandbox Code Playgroud)

Rob*_*ann 11

由于脚本按顺序执行(即使并行加载),您可以等待"最后一行"的脚本

head.js(
    { webfont  : 'http://ajax.googleapis.com/ajax/libs/webfont/1.0.31/webfont.js' },
    { jquery   : 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js' },
    { analytics: 'http://www.google-analytics.com/ga.js' }
);

 head.ready('analytics', function() {
    // when this triggers, webfont & jquery will have finished loading too
 });
Run Code Online (Sandbox Code Playgroud)