-
Notifications
You must be signed in to change notification settings - Fork 172
/
iload.js
48 lines (44 loc) · 1.16 KB
/
iload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Created by louiszhai on 17/6/19.
* description: Asynchronous sequential loading resources.
*/
const head = document.getElementsByTagName('head').item(0);
// 加载script标签
function loadScript(path, isSync, callback) {
const script = document.createElement('script');
script.async = !!isSync;
script.src = path;
if (typeof callback === 'function') {
script.addEventListener('load', callback);
}
head.appendChild(script);
}
// 异步加载顺序执行多个script脚本
const iload = (...rest) => {
let pathLength = rest.length;
let callback;
if (pathLength === 0) {
return false;
} else if (typeof rest[pathLength - 1] === 'function') {
pathLength--;
callback = rest[pathLength];
}
if (pathLength === 0) {
return false;
} else if (pathLength === 1) {
loadScript(rest[0], false, callback);
} else {
for (let i = 0; i < pathLength; i++) {
if (i === pathLength - 1) {
loadScript(rest[i], true, callback);
} else {
loadScript(rest[i], true);
}
}
}
};
export default {
install: (Vue, name = 'iload') => {
Object.defineProperty(Vue.prototype, name, { value: iload });
},
};