7

I'm experimenting with Modernizer.load.

I have this:

Modernizr.load([
      {
         load  : ['/js/jquery-1.6.1.js', '/js/jquery.tools.min.js', '/js/myscript.js']
      }
      ]);

If I understand correctly, I can use code like this to load scripts asynchronously. However, can I then execute them in order? What if myscript.js requires the jquery object to be loaded first?

In the example in the modernizr documentation, load([]) can take a 'complete' property, the parameter of which can be a function that can load another script when everything else is done. However, if I use a function here to load my post-dependancy script, then it loads in serial. The docs specifically say that this can harm perfomance.

However, if I load everything asynchronously, I don't have any idea about the order in which they run. And of course, I need my dependancies to run first.

4

1 回答 1

12

如果您使用Modernizr.load,您通过嵌入列表/哈希包含的所有文件都将被异步加载,但它们每个都将按照您放入它们的顺序执行。

因此,您的示例将异步加载文件,但按以下顺序执行它们:

1: /js/jquery-1.6.1.js
2: /js/jquery.tools.min.js
3: /js/myscript.js`

顺便说一句,您可以简化示例:

Modernizr.load(['/js/jquery-1.6.1.js', '/js/jquery.tools.min.js', '/js/myscript.js']);

有关更多详细信息,请参阅文档中的Modernizr.load() 教程,或查看Yepnopejs.com(此时 Modernizr.load() 基本上就是这样)。

于 2011-07-13T03:10:55.577 回答