Copy codeThe 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 ();
*/