[英]How can I block the execution of a script until another one is loaded?
Suppose I load these scripts from my host webpage : 假设我从主机网页加载这些脚本:
<script src="http://www.mywebsite.com/widget/widget.js?type=normal" type="text/javascript"></script>
<script src="http://www.mywebsite.com/widget/widget.js?type=rotation" type="text/javascript"></script>
and I'd like to execute the second one only when the first one have finished (totally; it can contain asynch functions). 并且我只想在第一个完成时执行第二个(完全;它可以包含异步函数)。
How can I do it? 我该怎么做?
You're already doing it right : the scripts are executed in the order of integration in the page, not loading. 您已经做得很好:脚本按照页面中的集成顺序执行,而不是加载。
EDIT : as MaxArt pointed, this may not be what you're looking for. 编辑:正如MaxArt指出的,这可能不是你想要的。 His answer is a good one. 他的答案很好。 But generally, you'll want to use javascript usual patterns and event based logic to avoid this kind of problems : 但一般来说,你会想要使用javascript常用模式和基于事件的逻辑来避免这种问题:
main
file launching this and calling the sequence of actions, the asynchronicity being handled via callbacks. 有一个main
文件启动它并调用动作序列,异步性通过回调处理。 My main code usually looks (a little) like this : 我的主要代码通常看起来像这样:
$(window).ready(function() {
var myBigEngine = new BigEngine();
myBigEngine.init(function(){
// do other things, no need to add other layers as user events and ajax message callback will do the rest
});
});
尝试将defer =“defer”属性应用于第二个<script>
声明
<script src="http://www.mywebsite.com/widget/widget.js?type=rotation" type="text/javascript" defer="defer" ></script>
Wrap the whole script in a function, like this: 将整个脚本包装在一个函数中,如下所示:
(function(id) {
var id = setInterval(function() {
if (!window.doneLoading) return;
clearInterval(id);
// The whole script file goes here
...
}, 50);
})();
The setInterval polls the (global, sorry) variable doneLoading
. setInterval轮询(全局,抱歉)变量doneLoading
。 In the first script, you have to set doneLoading
to true
or any other non-false value when your async function is completely loaded, like at the end of an AJAX request maybe? 在第一个脚本中,您必须在完全加载异步函数时将doneLoading
设置为true
或任何其他非假值,例如在AJAX请求结束时可能?
Edit : since I'm suggesting to add a global variable to the script, it may as well add a global function . 编辑 :因为我建议在脚本中添加一个全局变量,所以它也可以添加一个全局函数 。 So instead of setting up a setInterval
call, wrap the second script inside a function... but like this: 因此,不是设置setInterval
调用,而是将第二个脚本包装在函数中......但是像这样:
function doneLoading() {
// The whole script file goes here
...
}
In the first script file, at the end of your callback function, just call doneLoading()
. 在第一个脚本文件中,在回调函数结束时,只需调用doneLoading()
。
you can put second script init function in window.onload event 你可以在window.onload事件中放入第二个脚本init函数
window.onload = function(){
// call function from second script
}
or with jQuery 或者使用jQuery
$(function(){
// call function from second script
});
you can event add second script with this function 你可以使用此功能添加第二个脚本
function loadScript(src){
var f=document.createElement('script');
if(f){
f.setAttribute("type","text/javascript");
f.setAttribute("src",src);
document.getElementsByTagName("head")[0].appendChild(f);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.