Modernizr 可以异步加载脚本但按顺序执行它们吗?

发布于 2024-11-24 03:11:21 字数 471 浏览 2 评论 0原文

我正在尝试 Modernizer.load。

我有这样的:

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

如果我理解正确,我可以使用这样的代码异步加载脚本。但是,我可以按顺序执行它们吗?如果 myscript.js 要求首先加载 jquery 对象怎么办?

在 Modernizr 文档的示例中, load([]) 可以采用“完整”属性,该属性的参数可以是一个函数,该函数可以在其他所有操作完成后加载另一个脚本。但是,如果我在这里使用函数来加载我的依赖后脚本,那么它会串行加载。文档特别指出这可能会损害性能。

但是,如果我异步加载所有内容,我就不知道它们的运行顺序。当然,我需要首先运行我的依赖项。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的十字路口 2024-12-01 03:11:21

如果您使用 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() 基本上就是这样)。

If you use Modernizr.load, all the files you include via the embedded list/hash will be loaded asynchronously, but they’ll each be executed in the order that you put them in.

So, your example will load the files asynchronously but execute them in this order:

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

You can simplify your example, by the way:

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

For more details, see the Modernizr.load() tutorial in the Documentation, or check out Yepnopejs.com (which is what Modernizr.load() basically is, at this time).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文