Modernizr可以异步加载脚本但按顺序执行吗? [英] Can modernizr load scripts asynchronously but execute them in order?

查看:74
本文介绍了Modernizr可以异步加载脚本但按顺序执行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试Modernizer.load.

I'm experimenting with Modernizer.load.

我有这个:

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

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

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?

在modernizr文档的示例中,load([])可以具有'complete'属性,该属性的参数可以是一个函数,该函数可以在完成其他所有操作后加载另一个脚本.但是,如果我在此处使用函数加载依赖项后脚本,则它将以串行方式加载.文档特别指出,这可能会损害性能.

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.

推荐答案

如果您使用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']);

有关更多详细信息,请参见文档中的 Modernizr.load()教程,或查看 Yepnopejs.com (目前基本上是Modernizr.load()的内容).

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).

这篇关于Modernizr可以异步加载脚本但按顺序执行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆