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. Oracle配置SQL空间操作要点说明

    前面配置PL/SQL直接通过SQL查询SDE空间数据库,网上已有诸多示例, 常见问题如下: ORA-06520: PL/SQL: 加载外部库时出错ORA-06522: Unable to load D ...

  2. LoRa无线技术介绍

    什么是LoRa LoRa是semtech公司创建的低功耗局域网无线标准,低功耗一般很难覆盖远距离,远距离一般功耗高,要想马儿不吃草还要跑得远,好像难以办到.LoRa的名字就是远距离无线电(Long R ...

  3. oracle drop 表后 恢复

    1.查看回收站中表 select object_name,original_name,partition_name,type,ts_name,createtime,droptime from recy ...

  4. Petrozavodsk Winter Camp, Warsaw U, 2014, A The Carpet

    一个地图上有若干障碍,问允许出现一个障碍的最大子矩形为多大? 最大子矩形改编 #include<bits/stdc++.h> using namespace std; #define re ...

  5. 使用vue时,报错“exports is not defined”

    在开发中引用插件时,报错“exports is not defined” 但在引用第三方组件的时候,在浏览器中报错“exports is not defined”.根据浏览器报错信息,查询到报错来源是 ...

  6. ZedBoard前期准备工作

    1. 资源下载 内核:https://github.com/Xilinx/linux-xlnx/releases uboot:https://github.com/Xilinx/u-boot-xlnx ...

  7. ajax入门学习

    1.   XMLHttpRequest 是 AJAX 的基础. 所有现代浏览器均支持 XMLHttpRequest 对象(IE5 和 IE6 使用 ActiveXObject). XMLHttpReq ...

  8. printf()、sprintf()、vprintf()、vsprintf()(转)

    转自http://sumsung753.blog.163.com/blog/static/14636450120112151092934/ 一.printf() printf()函数优点在于可以格式化 ...

  9. Webpack学习-Plugin

    原文地址:http://wushaobin.top/2019/03/15/webpackPlugin/ 什么是Plugin? 在Webpack学习-工作原理(上)一文中我们就已经介绍了Plugin的基 ...

  10. learning makefile call func