我无法检查脚本是否已加载。
我尝试在脚本中放置自定义变量并使用.onload进行检查。我甚至设置了一个计时器来停止触发初始函数,但是我得到了以下错误
"未捕获的TypeError:undefined不是函数"
控制台中的光标指向new createjs.Container
以下是目前的代码:
此函数使用.loaded并将var设置为true 问题是他们总是如愿,但我仍然会遇到错误
unction politeInit(){
var s7Loaded, s6Loaded, s5Loaded, s4Loaded, s3Loaded, s2Loaded, s1Loaded;
var css = document.createElement('link');
css.setAttribute("rel","stylesheet");
css.setAttribute("type","text/css");
css.setAttribute("href","fonts/fonts.css");
document.getElementsByTagName('head')[0].appendChild(css);
var s1 = document.createElement('script');
s1.setAttribute("type","text/javascript");
s1.setAttribute("src","libs/easeljs-0.6.0.min.js");
var s2 = document.createElement('script');
s2.setAttribute("type","text/javascript");
s2.setAttribute("src","libs/tweenjs-0.4.0.min.js");
var s3 = document.createElement('script');
s3.setAttribute("type","text/javascript");
s3.setAttribute("src","libs/movieclip-0.6.0.min.js");
var s4 = document.createElement('script');
s4.setAttribute("type","text/javascript");
s4.setAttribute("src","libs/preloadjs-0.3.0.min.js");
var s5 = document.createElement('script');
s5.setAttribute("type","text/javascript");
s5.setAttribute("src","Mondeo_Lightning_728x90_V2.js");
var s6 = document.createElement('script');
s6.setAttribute("type","text/javascript");
s6.setAttribute("src","setUpText.js");
var s7 = document.createElement('script');
s7.setAttribute("type","text/javascript");
s7.setAttribute("src","main.js");
document.getElementsByTagName('head')[0].appendChild(s1);
document.getElementsByTagName('head')[0].appendChild(s2);
document.getElementsByTagName('head')[0].appendChild(s3);
document.getElementsByTagName('head')[0].appendChild(s4);
document.getElementsByTagName('head')[0].appendChild(s5);
document.getElementsByTagName('head')[0].appendChild(s6);
document.getElementsByTagName('head')[0].appendChild(s7);
s1.onload = function(){
s1Loaded = true;
}
s2.onload = function(){
s2Loaded = true;
}
s3.onload = function(){
s3Loaded = true;
}
s4.onload = function(){
s4Loaded = true;
}
s5.onload = function(){
s5Loaded = true;
}
s6.onload = function(){
s6Loaded = true;
}
s7.onload = function(){
s7Loaded = true;
}
var timeout = 100; // 10 seconds timeout
poll = function () {
setTimeout(function () {
timeout--;
if (s7Loaded && s6Loaded && s5Loaded && s4Loaded && s3Loaded && s2Loaded && s1Loaded) {
init();
}else if (timeout > 0) {
poll();
}else {
// External library failed to load
}
}, 100);
};
poll();
}
答案 0 :(得分:0)
您应该将脚本添加到正文中。
function politeInit () {
/* ... */
["libs/easeljs-0.6.0.min.js", "libs/tweenjs-0.4.0.min.js",
"libs/movieclip-0.6.0.min.js", "libs/preloadjs-0.3.0.min.js",
"Mondeo_Lightning_728x90_V2.js", "setUpText.js", "main.js"].
forEach(function (src) {
var script = document.createElement('script');
script.src = '' + src;
document.body.appendChild(script);
});
}
然后,在main.js
中,您应该有一个立即调用的函数表达式(IIFE)来启动页面执行。但是,其他js文件中任何立即调用的脚本都可以在另一个“线程”中并发运行。如果您打算使用这些脚本中的任何一个,则可能会导致竞争条件。如果是这种情况,请将这些脚本更改为不立即调用,而是从main.js
手动调用它们。这是IIFE的样子:
(function () {
/* do stuff immediately */
})();
它也可以是这样的:
!function () { /* do stuff */ };