- class Tools
- {
- static loadScript(url, callback)
- {
- let old_script = document.getElementById(url);
- if (old_script)
- {
- if (old_script.ready == true)
- {
-
- callback();
- return;
- }
- else
- {
- document.body.removeChild(old_script);
-
- }
- }
- let script = document.createElement('script');
- script.id = url;
- script.src = url;
- script.onload = script.onreadystatechange = function() {
- if (script.ready) {
- return false;
- }
- if (!script.readyState
- || script.readyState == "loaded" || script.readyState == 'complete'
- ) {
-
- script.ready = true;
- callback();
- }
- };
- document.body.appendChild(script);
- }
-
-
-
- static syncLoadScripts(scripts, callback)
- {
- var ok = 0;
- var loadScript = function(url) {
- Tools.loadScript(url, function(){
- ok++;
-
- if (ok == scripts.length) {
- callback();
- }
- else {
- loadScript(scripts[ok])
- }
- })
- }
- loadScript(scripts[0]);
- }
-
-
-
- static asyncLoadScripts(scripts, callback)
- {
- var ok = 0;
- for (var i=0; i < scripts.length; i++) {
- Tools.loadScript(scripts[i], function() {
- console.log(scripts[ok])
- ok++;
- if (ok==scripts.length)
- {
- callback();
- }
- })
- }
- }
- }
-
- export default Tools;
posted @
2018-06-14 09:54
大耳朵小虎
阅读(
385)
评论()
编辑
收藏
举报