Use controllers in js to load corresponding scripts

Source: Internet
Author: User
Tags getscript

My idea is to integrate these methods into $ when writing an independent module. in the tools object, next, on the page, call the Controller to load the module, and judge after loading. whether the number of objects in tools is equal to the number of modules loaded in a predefined definition. If the number of objects is not equal, the system waits for the objects. If the number is equal, the system executes the callback function.
Copy codeThe 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) {arglg = arglg-1; fn = arglast ;}
For (var I = 0; I <argleng; I ++ ){
Queue. 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! ');
});

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.