63 lines
1.6 KiB
JavaScript
63 lines
1.6 KiB
JavaScript
//jsloader.js
|
|
|
|
(function() {
|
|
|
|
var scriptManager= {};
|
|
scriptManager.loadingcount=0;
|
|
|
|
scriptManager.loadjs=async function(pth) {
|
|
var resolve;
|
|
var reject;
|
|
var ret=new Promise(function(s, r) {
|
|
resolve=s;
|
|
reject=r;
|
|
});
|
|
var sc=document.createElement("script");
|
|
sc.src=pth;
|
|
//sc.type="text/javascript";
|
|
if(document.readyState=="complete") {
|
|
document.body.appendChild(sc);
|
|
} else {
|
|
var hfn=function() {
|
|
window.removeEventListener("load", hfn);
|
|
document.body.appendChild(sc);
|
|
};
|
|
window.addEventListener("load", hfn);
|
|
}
|
|
|
|
sc.addEventListener(("readyState" in sc?"readystatechange":"load"), function() {
|
|
console.debug("script "+pth+" loaded successfully.");
|
|
scriptManager.loadingcount--;
|
|
sc.remove();
|
|
resolve();
|
|
});
|
|
|
|
sc.addEventListener("error", function() {
|
|
window.loadfail("script "+pth+" load failed");
|
|
sc.remove();
|
|
reject();
|
|
});
|
|
|
|
scriptManager.loadingcount++;
|
|
return ret;
|
|
};
|
|
|
|
scriptManager.isDone=function() {
|
|
return scriptManager.loadingcount==0;
|
|
};
|
|
|
|
scriptManager.waitAll=async function() {
|
|
return new Promise(function(resolve, reject) {
|
|
var i;
|
|
i=setInterval(function() {
|
|
if(scriptManager.loadingcount==0) {
|
|
clearInterval(i);
|
|
resolve();
|
|
}
|
|
}, 100);
|
|
});
|
|
};
|
|
|
|
window.scriptManager=scriptManager;
|
|
})();
|