forked from 1601com/SimpleAsyncLoaderJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleAsyncLoaderJS.js
More file actions
92 lines (80 loc) · 2.28 KB
/
Copy pathSimpleAsyncLoaderJS.js
File metadata and controls
92 lines (80 loc) · 2.28 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var SimpleAsyncLoaderJS = (function () {
/**
* @constructor
*/
var SimpleAsyncLoaderJS = function () {
SimpleAsyncLoaderJS.awaitDOMCall(SimpleAsyncLoaderJS._initLoader);
}
/**
* @param {function} f
*/
SimpleAsyncLoaderJS.awaitDOMCall = function (f) {
if (document.readyState !== 'loading') {
SimpleAsyncLoaderJS.timeoutCall(f);
} else {
document.addEventListener('DOMContentLoaded', function domContentLoaded() {
SimpleAsyncLoaderJS.timeoutCall(f);
document.removeEventListener("DOMContentLoaded", domContentLoaded, false);
}, false);
}
}
/**
* @param {function} f
*/
SimpleAsyncLoaderJS.timeoutCall = function (f) {
if (typeof f !== "function") {
return;
}
setTimeout(function () {
SimpleAsyncLoaderJS.call(f);
});
}
/**
* @param {function} f
*/
SimpleAsyncLoaderJS.call = function (f) {
if (typeof f !== "function") {
return;
}
f();
}
/**
*
*/
SimpleAsyncLoaderJS.loadAsyncDataScriptFunctions = function () {
var dataScripts = document.querySelectorAll('script[data-script-function]');
for (var i = 0; i < dataScripts.length; i++) {
SimpleAsyncLoaderJS.call(window[dataScripts[i].getAttribute("data-script-function")]);
}
}
/**
*
*/
SimpleAsyncLoaderJS.loadAsyncFunctionsArray = function () {
if (typeof asyncFunctions === "undefined" || !Array.isArray(asyncFunctions)) {
return;
}
for (var j = 0; j < asyncFunctions.length; j++) {
SimpleAsyncLoaderJS.call(asyncFunctions[j]);
}
}
/**
* @return boolean
*/
SimpleAsyncLoaderJS._initLoader = function () {
SimpleAsyncLoaderJS.loadAsyncDataScriptFunctions();
SimpleAsyncLoaderJS.loadAsyncFunctionsArray();
return true;
}
/**
* @param asyncFunction
* @return {SimpleAsyncLoaderJS}
*/
SimpleAsyncLoaderJS.create = function (asyncFunction) {
return new SimpleAsyncLoaderJS(asyncFunction);
}
return SimpleAsyncLoaderJS;
})();
(function () {
SimpleAsyncLoaderJS.create();
})();