CopyCode The Code is as follows: (function ($ ){
(Function ($ ){
$. Preload = function (data, CFG ){
Return new loader (data, CFG );
};
VaR maps ={}, on = $. event. Add, UN = $. event. Remove, head = Document. getelementsbytagname ('head') [0], body =
Document. Body, BS = $. browser, ie = BS. MSIE, WebKit = BS. WebKit, Gecko = BS. Mozilla, Space = 1000, Ajax =
$. Ajax,
Loaders = $. preload. loaders = {
'Js': function (URL, callback, timeout, defer ){
VaR S, timer;
If (defer ){
If (IE ){
Return loaders. IMG (URL, callback, timeout );
} Else {
S = Document. createelement ('object ');
S. Data = URL;
S. width = S. Height = 0;
}
} Else {
S = Document. createelement ('script ');
S. setattribute ('type', 'text/JavaScript ');
S. setattribute ('src', URL );
}
Function f (){
If (timer)
Cleartimeout (timer );
S. onreadystatechange = S. onload = S. onerror = NULL;
Callback (URL, false );
}
If (IE ){
S. onreadystatechange = function (){
If (this. readystate = 'loaded' | this. readystate = 'complete '){
If (timer)
Cleartimeout (timer );
S. onreadystatechange = NULL;
Callback (URL, true );
}
};
} Else {
S. onload = function (){
If (timer)
Cleartimeout (timer );
S. onload = S. onerror = NULL;
Callback (URL, true );
};
S. onerror = F;
}
Timer = setTimeout (F, timeout );
Body. appendchild (s );
},
'Css ': function (URL, callback, timeout, defer ){
If (defer ){
Return loaders. js (URL, callback, timeout, defer );
}
VaR S = Document. createelement ('link'), timer;
S. setattribute ('rel ', 'stylesheet ');
S. setattribute ('type', 'text/CSS ');
S. setattribute ('href ', URL );
Function f (){
If (timer)
Cleartimeout (timer );
S. onreadystatechange = S. onload = S. onerror = NULL;
Callback (URL, false );
}
If (IE ){
S. onreadystatechange = function (){
If (this. readystate = 'loaded' | this. readystate = 'complete '){
If (timer)
Cleartimeout (timer );
S. onreadystatechange = NULL;
Callback (URL, true );
}
};
Timer = setTimeout (F, timeout );
} Else if (WebKit | gecko ){
Timer = new date ();
Function f (){
If ('sheet 'in S) & ('cssrules' in S. Sheet )){
Try {
Callback (URL ,!! S.sheet.css rules [0]);
} Catch (e ){
SetTimeout (F, space );
}
} Else if (new date ()-timer> timeout ){
Callback (URL, false );
} Else {
SetTimeout (F, space );
}
}
SetTimeout (F, space * 2 );
} Else {
S. onload = function (){
If (timer)
Cleartimeout (timer );
S. onload = S. onerror = NULL;
Callback (URL, true );
};
S. onerror = F;
Timer = setTimeout (F, timeout );
}
Head. appendchild (s );
},
'Img ': function (URL, callback, timeout ){
VaR S = new image (), timer;
Function f (){
If (timer)
Cleartimeout (timer );
S. onload = S. onerror = NULL;
Callback (URL, false );
}
S. onload = function (){
If (timer)
Cleartimeout (timer );
S. onload = S. onerror = NULL;
Callback (URL, true );
};
S. onerror = F;
Timer = setTimeout (F, timeout );
S. src = URL;
},
'Ajax ': function (URL, callback, CFG ){
CFG = CFG | {};
Cfg. url = URL;
Cfg. Success = function (data ){
Callback (URL, true, data );
};
Cfg. Error = function (){
Callback (URL, false );
};
Ajax (CFG );
}
};
Function loader (data, CFG ){
VaR self = This, cur =-1, items = [], pendings = [], done, I = 0, L = data. length, J, M, S, T, C, D, TT, item, doing =
0, load;
CFG = CFG | {};
For (; I <L; ++ I ){
Item = data [I];
If (typeof item = 'string '){
S = item. substr (item. lastindexof ('.') + 1 );
Items. Push (maps [item] = {
Type: loaders [s]? S: 'img ',
URL: item
});
} Else if (item. URLs ){
For (j = 0, S = item. type, t = item. Require, c = item. Callback, D = item. Defer, TT = item. Timeout, item =
Item. URLs, M = item. length; j <m; ++ J ){
S = S | item [J]. substr (item [J]. lastindexof ('.') + 1 );
Items. Push (maps [item [J] = {
Type: loaders [s]? S: 'img ',
URL: item [J],
Require: T,
Callback: C,
Defer: D,
Timeout: TT
});
}
} Else {
If (! Item. Type ){
S = item. url. substr (item. url. lastindexof ('.') + 1 );
Item. type = loaders [s]? S: 'img ';
}
Items. Push (maps [item. url] = item );
}
}
This. Success = This. Fail = This. Progress = 0;
If (CFG. onfinish)
This. onfinish = cfg. onfinish;
Timeout = cfg. Timeout || 2000;
Function callback (URL, flag, data ){
If (FLAG ){
++ Self. success;
} Else {
++ Self. fail;
}
Self. Progress = (self. Success + self. Fail)/items. length;
Console.info (URL );
Console. Warn (FLAG );
Item = maps [url];
Item. Success = flag;
If (self. Progress = 1 ){
Self. Stop ();
}
If (item. Parent &&! Item. Defer &&! Cfg. Defer ){
$ (Item. Parent) [0]. innerhtml = data | '';
}
If (item. Callback ){
Item. Callback (data );
}
Item. Done = true;
-- Doing;
}
Function runnable (item, pend ){
VaR it;
If (typeof item. Require === 'string '){
If (item. Done)
Return false;
If (! Item. Require)
Return true;
It = maps [item. Require];
If (! It | it. Done ){
If (pend)
Pendings. Shift ();
If (IT & it. Success ){
Return true;
} Else {
Callback (item. url, false );
}
} Else if (! Pend ){
Pendings. Push (item );
}
} Else {
For (IT = item. length; it --;){
If (! Runnable (item [it], pend ))
Return false;
}
Return true;
}
}
Function run (){
VaR item = pendings [0];
If (! Item |! Runnable (item, true )){
While (item = items [++ cur]) {
If (runnable (item )){
Break;
}
}
}
If (item ){
VaR fn = loaders [item. type | 'img '];
If (FN ){
++ Doing;
If (item. type = 'ajax '){
If (item. cfg &&! Item. cfg. Timeout)
Item. cfg. Timeout = timeout;
FN (item. url, callback, item. cfg );
} Else {
FN (item. url, callback, item. Timeout | timeout, item. Defer === undefined? Cfg. defer
: Item. Defer );
}
};
If (load ){
Run ();
} Else {
Self. Timer = setTimeout (run, space );
}
} Else if (pendings. Length ){
Self. Timer = setTimeout (run, space );
}
}
This. Start = function (Delay ){
If (! Done)
This. Timer = setTimeout (run, delay> space? Delay: space );
};
This. Stop = function (){
If (this. Timer ){
Cleartimeout (this. Timer );
This. Timer = NULL;
Done = true;
If (this. onfinish ){
If (! Doing)
This. onfinish ();
Else {
S = setinterval (function (){
If (! Doing ){
Clearinterval (s );
Self. onfinish ();
}
}, Space );
}
}
}
};
This. Pause = function (){
Cleartimeout (this. Timer );
};
This. Resume = function (){
This. Timer = setTimeout (run, space );
};
This. load = function (){
Cleartimeout (this. Timer );
Load = true;
Run ();
};
}
}) (Jquery );
/**
* @ Example
* Var loader = $. preload ([
// String, with default configuration
'1.jpg ', '1. js ',
// Object, custom configuration, such as type, require, timeout, defer, callback
{
Type: 'img ',
URL: 'http: // foo.com/foo ',
Timeout: 10
},{
URL: '3. js ',
Callback: FN,
Defer: True,
Require: '1. js'
},
// Object. You can use URLs to specify a group of identical configurations.
{
Type: 'css ',
URLs: Using '4.css ', '5.css']
}], {
// Call after loading
Onfinish: FN,
// Loading timeout
Timeout: 50
});
// Start pre-loading
Loader. Start ();
Loader. Stop ();
// Pause the pre-load.
Loader. Pause ();
Loader. Resume ();
// Real-time loading
Loader. Load ();
*/