【发布时间】:2017-11-03 06:07:08
【问题描述】:
我正在开发 jqueryui 插件。 我有一个脚本,它检查是否未添加某些库然后添加它。我正在使用 setTimeOut 按顺序添加库。这是我的代码:
if(typeof jQuery === 'undefined'){ //check if jQuery Exists
console.log("jQuery not loaded");
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src",
"http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js");
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
console.log("jQuery loaded");
}
setTimeout(function(){
if (typeof jQuery.ui === "undefined"){
console.log("jQueryUI not loaded");
var script_tag2 = document.createElement('script');
script_tag2.setAttribute("type","text/javascript");
script_tag2.setAttribute("src",
"http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js");
(document.getElementsByTagName("head")[0] ).appendChild(script_tag2);
}
}, 500);
但问题是因为有很多库,所以需要太多时间。如果我减少时间,那么我会得到错误,即没有添加以前的库。而不是将数字传递给 settimeout 函数。我想传递一个函数来检查是否加载了以前的库然后添加这个库。 我希望这能让您了解我的查询。
谢谢
【问题讨论】:
-
监听
onLoad脚本元素事件... -
看起来你正在重塑requirejs.org。
标签: javascript jquery jquery-ui jquery-plugins