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文件的更多相关文章

  1. 自己编写jQuery动态引入js文件插件 (jquery.import.dynamic.script)

    这个插件主要是结合jquery或者xhr异步请求来使用的,它可以把已经引入过的js文件记录在浏览器内存中,当下次再引入相同的文件就忽略该文件的引入. 此插件不支持浏览器刷新保存数据,那需要利用cook ...

  2. Extjs学习----------动态载入js文件(减轻浏览器的压力)

    动态载入js文件能够减轻浏览器的压力,本例使用了Ext.window.Window组件,该组件的学习地址:http://blog.csdn.net/z1137730824/article/detail ...

  3. 为了提高性能,怎样动态载入JS文件

    超级表格是一款多人协作的在线表格.程序相当复杂,用到十几个JS文件. 可是有些文件是在打开某些类型的表格时才须要载入. 比如,仅仅有当打开甘特图表格时,才须要载入gantetu.js文件. 那么问题来 ...

  4. asp.net后台代码动态添加JS文件和css文件的引用

    首先添加命名空间 using System.Web.UI.HtmlControls; 代码动态添加css文件的引用 HtmlGenericControl myCss = new HtmlGeneric ...

  5. vue中如何在本地导入js文件

    import {setStore,setUser,getStore,removeStore} from "../../../public/localstory" 在导入js文件时, ...

  6. Boostrap本地导入js文件

    我一般都是用CDN直接导入的,但是有时候需要自己添加一些功能进入,会用到本地导入.关于导入路径问题,做个笔记. 使用HBuilder,首先右键导入相应的js/cs文件 然后是常规——>文件系统 ...

  7. 动态添加js文件.

    方法一: $.getScript(url,callback); 这个方法是对$.ajax({ })的封装.默认是异步的而且是带有缓存的. 缓存对于用户来说,是个好东西,但是对于开发者来说可就是日了狗的 ...

  8. 动态添加JS文件到页面

    /*** ** 功能: 加载外部JS文件,加载完成后执行回调函数callback ***/ var utools = { config: { id: "", url: " ...

  9. jquery----语法扩展(导入js文件)

    简单使用 第一步,新建js文件 第二步,在js文件中添加 $.extend({ "GDP": function () { console.log("哈哈哈哈") ...

随机推荐

  1. 自定义Cordova插件(基础篇)

    cordova自定义插件 注意:存放自定义cordova插件目录不能有空格可能会报错 cordova的安装 下载node.js,安装完成后你可以在命令行中使用node和npm. 安装cordova使用 ...

  2. linux下初始化mysql时报错

    执行mysqld --initialize后报错 报错内容: 019-04-24 18:07:59 0 [Warning] TIMESTAMP with implicit DEFAULT value ...

  3. C语言函数声明什么时候可以省略,什么时候不能省?

    在学习C语言函数的时候,老师总会告诉我们函数要写声明,然后再定义.这是个稳健的做法.等我自己学习了其他高级语言以后,回头再来写C,突然就觉得函数要写声明有点麻烦.无意间发现有一次函数没写声明居然编译( ...

  4. NPM 报错--fs: re-evaluating native module sources is not supported. If you are using the graceful-fs module

    fs: re-evaluating native module sources is not supported. If you are using the graceful-fs module 解决 ...

  5. JS数组映射详解

    现在这里占个坑位,免的忘了,需要整理一下最近的内容: 1.数组映射的使用 2.微信分享功能详解 3.jq自己封装 4.HTML的富文本应用

  6. 将VS项目提交至SVN时,怎样忽略bin和obj目录中的文件

    方法一: 通过设置SVN的Global ignore pattern值. 使用下面的设定值: *.o *.lo *.la *.al .libs *.so *.so.[0-9]**.a *.pyc *. ...

  7. django自定义模板标签

    # 创建自定义模板标签目录 django_project_name app_name templatetags (创建Python Packge,注意一定要用templatetags这个名字) my_ ...

  8. 利用Delphi编写Socket通信程序

    一.Delphi与Socket 计算机网络是由一系列网络通信协议组成的,其中的核心协议是传输层的TCP/IP和UDP协议.TCP是面向连接的,通信双方保持一条通路,好比目前的电话线,使用telnet登 ...

  9. winfrom程序Datagridview列名问题

    之前在做程序的时候,有遇到过这个问题: 无法将类型“string”隐式转换为“System.Windows.Forms.DataGridViewTextBoxColume"解决方法 解决办法 ...

  10. python 生成器(generator)的生成方式

    generator包括生成器和带yield的generator函数. 写了一个生成杨辉三角的小例子: # -*- coding:utf-8 -*- def triangles(): l = [1] w ...