如何管理 JavaScript 中的依赖关系?
我的脚本需要等待满足某些条件才能运行 - 例如等待另一个脚本加载,或等待创建数据对象。
我该如何管理此类依赖关系?我能想到的唯一方法是使用 setTimeout 以短间隔循环并检查函数或对象是否存在。有更好的办法吗?
如果 setTimeout 是唯一的选择,那么轮询我的页面的合理时间间隔是多少? 50 毫秒、100 毫秒?
[编辑]我的一些脚本从页面本身或从 Web 服务收集数据,有时从多个来源的组合收集数据。数据可以随时准备好,无论是在页面加载之前还是之后。其他脚本呈现数据(例如构建图表)。
[更新]感谢您提供有用的答案。我同意我不应该重新发明轮子,但如果我使用一个库,至少我想了解背后的逻辑(这只是一个花哨的超时吗?)来尝试预测对我的页面的性能影响。
I have scripts that needs to wait for certain conditions to be met before they run - for example wait for another script to be loaded, or wait for a data object to be created.
How can I manage such dependencies? The only way I can think of is to use setTimeout to loop in short intervals and check the existence of functions or objects. Is there a better way?
And if setTimeout is the only choice, what is a reasonable time interval to poll my page? 50 ms, 100 ms?
[Edit] some of my scripts collect data, either from the page itself or from Web services, sometimes from a combination of multiple sources. The data can be ready anytime, either before or after the page has loaded. Other scripts render the data (for example to build charts).
[update] thanks for the useful answers. I agree that I shouldn't reinvent the wheel, but if I use a library, at least I'd like to understand the logic behind (is it just a fancy timeout?) to try and anticipate the performance impact on my page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在正在加载的脚本末尾进行类似
loaded(xyz);
的函数调用。该函数将在其他地方定义,并设置为根据 xyz 的值调用已注册的回调。 xyz 可以是任何东西,一个识别脚本的简单字符串,或者一个复杂的对象或函数等等。或者只是使用
jQuery.getScript(url [, success(data, textStatus)] )
。You could have a function call like
loaded(xyz);
at the end of the scripts that are being loaded. This function would be defined elsewhere and set up to call registered callbacks based on the value ofxyz
.xyz
can be anything, a simple string to identify the script, or a complex object or function or whatever.Or just use
jQuery.getScript(url [, success(data, textStatus)] )
.对于相互依赖的脚本,请使用像 RequireJS 这样的模块系统。
要远程加载数据,请使用回调,例如
以下是这两者组合的示例:
For scripts that have dependencies on each other, use a module system like RequireJS.
For loading data remotely, use a callback, e.g.
Here's an example of these two in combination:
对于这种事情有很多框架。
我现在正在使用 Backbone http://documentcloud.github.com/backbone/
朋友有还推荐knockout.js http://knockoutjs.com/
这两个都使用MVC模式在数据更新后更新视图已被模型加载
[更新] 我认为在最基本的层面上,这些库使用回调函数和事件侦听器来更新页面的各个部分。
例如
There are many frameworks for this kind of thing.
I'm using Backbone at the moment http://documentcloud.github.com/backbone/
Friends have also recommended knockout.js http://knockoutjs.com/
Both of these use an MVC pattern to update views once data has been loaded by a model
[update] I think at their most basic level these libraries are using callback functions and event listeners to update the various parts of the page.
e.g.
我使用过 pxLoader,一个 JavaScript 预加载器,效果非常好。它默认使用 100ms 轮询。
我不会在这里重新发明轮子,除非你需要一些真正自定义的东西,所以给它(或任何真正的 JavaScript 预加载器库)看看。
I've used pxLoader, a JavaScript Preloader, which works pretty well. It uses 100ms polling by default.
I wouldn't bother reinventing the wheel here unless you need something really custom, so give that (or any JavaScript preloader library really) a look.