动态导入Js文件
var ScriptLoader = {
worker: ,
isWait: false,
readyQueue: [],
callback: [],
timer: null,
wait: function () {
if (!this._isComplateTask()) {
this.isWait = true;
this.readyQueue.unshift('wait');
}
//console.log('wait is true');
return this;
},
_isComplateTask: function () {
return this.worker === ;
},
loadJs: function (url, async, callback) {
console.log('load js ' + url);
if (this.isWait) {
// 将js加载到队列
this.readyQueue.unshift(url);
this.callback.unshift(callback);
if (!this.timer) {
// 定时处理队列
var that = this;
this.timer = setInterval(function () {
if (that.readyQueue.length === ) {
// 队列消费完, 清除定时器
clearInterval(that.timer);
that.timer = null;
} else if (that._isComplateTask()) {
that._loadReady();
}
}, );
}
} else {
this._realLoad(url, async, callback);
}
return this;
},
/**
* 消费队列
*/
_loadReady: function () {
var url;
while (this.readyQueue.length > ) {
url = this.readyQueue.pop();
if (url === 'wait') {
if (!this._isComplateTask()) {
this.isWait = true;
break;
}
} else {
this._realLoad(url, true, this.callback.pop());
}
}
},
_realLoad: function (url, async, callback) {
this.worker++;
var that = this;
console.log('start load js ' + url);
this._loadJsFile(url, function () {
that.worker--;
if (that.worker === ) {
//console.log('wait is false');
that.isWait = false;
}
if(callback){
callback();
}
}, async);
},
_loadJsFile: function (file, callback, async) {
var head, d = document;
((head = d.getElementsByTagName('head')[]) || (head = d.body.parentNode.appendChild(d.createElement("head"))));
var script = d.createElement("script");
script.type = "text/javascript";
script.src = file;
script.charset = 'UTF-8';
if (async) {
script.async = true;
}
if (script.readyState) {//IE
script.onreadystatechange = function () {
if (script.readyState === "loaded" || script.readyState === "complete") {
script.onreadystatechange = null;
callback();
}
};
} else {//其他浏览器
script.onload = function () {
callback();
};
}
head.appendChild(script);
}
};

动态导入Js文件的更多相关文章
- 自己编写jQuery动态引入js文件插件 (jquery.import.dynamic.script)
这个插件主要是结合jquery或者xhr异步请求来使用的,它可以把已经引入过的js文件记录在浏览器内存中,当下次再引入相同的文件就忽略该文件的引入. 此插件不支持浏览器刷新保存数据,那需要利用cook ...
- Extjs学习----------动态载入js文件(减轻浏览器的压力)
动态载入js文件能够减轻浏览器的压力,本例使用了Ext.window.Window组件,该组件的学习地址:http://blog.csdn.net/z1137730824/article/detail ...
- 为了提高性能,怎样动态载入JS文件
超级表格是一款多人协作的在线表格.程序相当复杂,用到十几个JS文件. 可是有些文件是在打开某些类型的表格时才须要载入. 比如,仅仅有当打开甘特图表格时,才须要载入gantetu.js文件. 那么问题来 ...
- asp.net后台代码动态添加JS文件和css文件的引用
首先添加命名空间 using System.Web.UI.HtmlControls; 代码动态添加css文件的引用 HtmlGenericControl myCss = new HtmlGeneric ...
- vue中如何在本地导入js文件
import {setStore,setUser,getStore,removeStore} from "../../../public/localstory" 在导入js文件时, ...
- Boostrap本地导入js文件
我一般都是用CDN直接导入的,但是有时候需要自己添加一些功能进入,会用到本地导入.关于导入路径问题,做个笔记. 使用HBuilder,首先右键导入相应的js/cs文件 然后是常规——>文件系统 ...
- 动态添加js文件.
方法一: $.getScript(url,callback); 这个方法是对$.ajax({ })的封装.默认是异步的而且是带有缓存的. 缓存对于用户来说,是个好东西,但是对于开发者来说可就是日了狗的 ...
- 动态添加JS文件到页面
/*** ** 功能: 加载外部JS文件,加载完成后执行回调函数callback ***/ var utools = { config: { id: "", url: " ...
- jquery----语法扩展(导入js文件)
简单使用 第一步,新建js文件 第二步,在js文件中添加 $.extend({ "GDP": function () { console.log("哈哈哈哈") ...
随机推荐
- Mysql 索引之B+tree
InnoDB使用的是聚簇索引,将主键组织到一棵B+树中,而行数据就储存在叶子节点上,若使用"where id = 14"这样的条件查找主键,则按照B+树的检索算法即可查找到对应的叶 ...
- HDU 1257 最少拦截系统(思路题)
Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高 ...
- angular + socket.io+nodejs
一.服务器端: 基本和nodejs工程相同 https://www.cnblogs.com/xuanmanstein/p/10509445.html 安装socket.io npm i --save ...
- eclipse运行项目,tomcat报错:Exception in thread :http-bio-8080-exec-4
eclipse运行项目,tomcat报错:Exception in thread :http-bio-8080-exec-4 转自 https://www.cnblogs.com/yby-blogs/ ...
- Robin Hood CodeForces - 672D (二分)
大意: 给定数组$a$, 每次操作使最大元素减小1最小元素增大1, 求k次操作后最大值与最小值的差. 二分出k次操作后最大值的最小值以及最小值的最大值, 若和能平分答案即为$max(0,R-L)$, ...
- Android 正则表达式实例
editText正则表达式的使用 检查输入是否符合规则 import Android.app.Activity; import android.os.Bundle; import android.vi ...
- 免费赠送原创的opengl电子书教程和案例源码
免费赠送原创的opengl电子书和案例源码,有兴趣qq群 52391108 下面是一部分教程截图
- SQL语句全解,非常棒!
链接自W3school非常详细的SQL教程 http://www.w3school.com.cn/sql/index.asp
- js设置radio单选框值选中
html页面: <div> <label><input type="radio" name="sex" value="m ...
- php 查询mysql数据批量转为PDF文件一(mac使用配置wkhtmltopdf html导出PDF)
数据转标准PDF查文档,查资料先转HTML标准格式再html转PDF 转PDF wkhtmltopdf工具是最佳选择 首先下载wkhtmltopdf https://wkhtmltopdf.org/d ...