同时并行加载? [英] Loaded Simultaneously in Parallel?

查看:130
本文介绍了同时并行加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script type="text/javascript" src="/scripts/a.js"></script>
<script type="text/javascript" src="/scripts/b.js"></script>

我有两个简单的JS脚本,在上面的顺序加载。的 a.js 的有一个函数的引用例如富()中的 b.js 的即的 a.js 的调用富()是在 b.js 的定义。在某些服务器上,被抛出一个错误,因为富()不是时执行的 a.js 的认可;在其它一些机器,被抛出任何错误。

I have two simple JS scripts, loaded in the above order. a.js has a reference to a function e.g. foo() in b.js i.e. a.js invokes foo() that is defined in b.js. On some servers, an error was thrown because foo() isn't recognized when a.js is executed; on some other machines, no error was thrown.

在做并行同时加载这两个脚本?或者,在序列?确实答案依​​赖于服务器的体系结构(例如,多芯,等等)?

Do those two scripts loaded simultaneously in parallel? Or, in sequence? Does the answer depend on the architecture of the servers (e.g. multi-core, etc.)?

感谢。

推荐答案

下载可能会或可能不会在平行(取决于浏览器),但这些脚本的解析是它们在页面上的布局的顺序

Downloads may or may not be in parallel (depending on the browser), but parsing of the scripts are in the sequence they are laid on the page.

如果您使用的顺序( a.js b.js ),其中富() b.js ,但叫 a.js ,这将导致一个错误,因为富()被称为它的存在了。

if you used that order (a.js before b.js), with foo() declared in b.js but called in a.js, this will result in an error because foo() was called before it existed.

这就是为什么JS库鼓励你之前的任何用户脚本装载他们的剧本一样的道理。这样一来,你使用它们之前就已存在的引用。

It's the same reason why JS libraries encourage you to load their scripts before any user scripts. That way, their references exist before you use them.

这篇关于同时并行加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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