JS中的动态脚本加载器 [英] dynamic script loader in JS
本文介绍了JS中的动态脚本加载器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我该如何编写动态MULTIPLE脚本加载程序,其中包含完整的处理程序,例如google
google.load(http:// script1 );
google.load(http:// script2);
google.setOnLoadCallback(function(){});
谢谢
解决方案我写了这样的
myApp.Loader = function(){
var queries = [] ;
var q = 0;
var p = 0;
var started = false;
var _callback = function(){};
var start = function(){
if(queries.length> 0&&!started){
started = true;
load(queries.shift());
} else if(queries.length> 0&&& amp;& amp; amp; start){
load(queries.shift());
} else if(queries.length == 0&& amp; amp; start){
started = false;
if(q> 0&& q == p){
callback();
}
}
};
var load = function(fullUrl){
$ .getScript(fullUrl,function(){
p ++;
start();
}) ;
};
var callback = function(){
_callback();
};
this.setCallback = function(fnc){
_callback = fnc;
if(q> 0&& q == p){
callback();
}
};
this.addQuery = function(query){
queries.push(query);
q ++;
if(!started){
start();
}
};
返回这个;
}
var Loader = new myApp.Loader();
myApp.load = function(fullUrl){
Loader.addQuery(fullUrl);
}
myApp.setOnLoadCallback = function(fnc){
Loader.setCallback(fnc);
}
并称之为
myApp.load( HTTP:// SCRIPT1);
myApp.load(http:// script2);
myApp.load(http:// script3);
myApp.setOnLoadCallback(function(){
//完成脚本加载处理
});
How can i write dynamic MULTIPLE script loader with complete handler like google
google.load("http://script1");
google.load("http://script2");
google.setOnLoadCallback(function(){});
thanks
解决方案
I wrote like that
myApp.Loader = function(){
var queries = [];
var q = 0;
var p = 0;
var started = false;
var _callback = function(){};
var start = function(){
if(queries.length > 0 && !started){
started = true;
load(queries.shift());
} else if(queries.length > 0 && started){
load(queries.shift());
} else if(queries.length == 0 && started){
started = false;
if(q > 0 && q == p){
callback();
}
}
};
var load = function(fullUrl){
$.getScript(fullUrl, function() {
p++;
start();
});
};
var callback = function(){
_callback();
};
this.setCallback = function(fnc){
_callback = fnc;
if(q > 0 && q == p){
callback();
}
};
this.addQuery = function(query){
queries.push(query);
q++;
if(!started) {
start();
}
};
return this;
}
var Loader = new myApp.Loader();
myApp.load = function(fullUrl){
Loader.addQuery(fullUrl);
}
myApp.setOnLoadCallback = function(fnc){
Loader.setCallback(fnc);
}
and call it
myApp.load("http://script1");
myApp.load("http://script2");
myApp.load("http://script3");
myApp.setOnLoadCallback(function(){
// complete script load handling
});
这篇关于JS中的动态脚本加载器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文