异步顺序加载 JavaScript 脚本队列

// 动态加载脚本文件
function getScript (url, success) {

var readyState = false,
script = document.createElement('script');
script.charset = 'utf-8';
script.src = url;

// attach handlers for all browsers
script.onload = script.onreadystatechange = function () {
if (!readyState && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
readyState = true;
success && success();
}
};
document.body.appendChild(script);

}
// 异步顺序加载队列
function use (res, callback) {

if (typeof res === 'string') {
var _res = res;
res = [];
res.push(_res);
}

var self = this,
queue = function (fs, cb) {
getScript(fs.shift(), function () {
fs.length ? queue(fs, cb) : cb && cb();
});
};

// execute the queued resources
queue(res, callback);

}

dawei

【声明】:北京站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。