function loadJS(url, callback) {
var head = window.document.getElementsByTagName("head")[0];
var script = window.document.createElement('script');
script.onload = script.onreadystatechange = script.onerror = function() {
if (script && script.readyState && /^(?!(?:loaded|complete)$)/.test(script.readyState)) return;
script.onload = script.onreadystatechange = script.onerror = null;
script.src = '';
script.parentNode.removeChild(script);
script = null;
if (callback) {
callback();
}
};
script.charset = "utf-8";
script.async = true;
script.src = url;
try {
head.appendChild(script);
} catch (exp) {}
}
function loadJS2(url,callback){
var head=document.getElementsByTagName('head')[0];
var script=document.createElement('script');
if(script.readyState){
script.onreadystatechange=function(){
if(script.readyState=='loaded' || script.readyState=='complete'){
script.parentNode.removeChild(script);
callback && callback();
}
};
}else{
script.onload=function(){
script.parentNode.removeChild(script);
callback && callback();
};
}
script.src=url;
head.appendChild(script);
}
console.time('start');
for(var i=0; i<100; i++){
loadJS2('https://code.jquery.com/jquery-3.2.1.js',function(){
console.log('count:'+i);
});
}
console.timeEnd('start');
网友评论