Home  >  Article  >  Web Front-end  >  Use the controller to load the corresponding script under js_javascript skills

Use the controller to load the corresponding script under js_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:22:44868browse

My idea is that when writing independent modules, pay attention to integrating these methods into the $.tools object. Next, in the page, call the controller to load the module, and judge the objects in $.tools after the loading is completed. Is the number equal to the predefined number of loaded modules? If not, continue to wait. If equal, execute the callback function.

Copy code The code is as follows:

/*
* LOADScript Mod
* Params url1,url2,url3,url4,fn
*/
jQuery.extend({
loadMod: function(){
var argleng = arguments.length,
arglast = arguments[argleng -1],
fn = false,
queue = [],
checknum = 0,
timer = null

// init
if(jQuery.isFunction( arglast)){argleng = argleng -1;fn=arglast;}
for (var i=0;iqueue.push(arguments[i]);
}

// getscript
jQuery.each(queue,function(i,o){
jQuery.getScript(o);
});

// check load ready?
loadReady();

function loadReady(){
if(jQuery.tools != undefined){
checknum = 0;
$.each(jQuery.tools ,function(i,n){
if(jQuery.isPlainObject(n)){checknum ;}
});
}
if(checknum != argleng){
clearTimeout( timer);
timer = setTimeout(loadReady, 100);
}else{
if(!!fn){fn.call(document.jQuery)}
}

}
}
});

// Usage
$.loadMod('a.js','b.js','c.js',function(){
alert('success!');
});
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn