Skip to content

Commit

Permalink
改善了加载大量html的性能
Browse files Browse the repository at this point in the history
改善了加载大量html的性能
  • Loading branch information
tmplink committed Apr 8, 2019
1 parent ce3c764 commit e55324b
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions domloader.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/*!
* domloader.js
* v2.2
* v2.3
* https://github.com/tmplink/domloader/
*
* Licensed GPLv3 © TMPLINK STUDIO
*/

var domloader = {
queue: [],
queue_async:[],
queue_after: [],
queue_preload: [],
loading_page: false,
Expand All @@ -22,19 +23,6 @@ var domloader = {
animation_time: 500,
animation_stime: 0,

html: function (dom, path) {
domloader.id++;
domloader.log('Include::HTML::' + path);
domloader.queue.push(
function () {
$.get(domloader.root + path, {v: domloader.version}, function (response) {
$(dom).replaceWith(response);
domloader.load(path);
}, 'text');
}
);
},

preload: function (path) {
domloader.id++;
domloader.log('Preload::' + path);
Expand All @@ -60,6 +48,19 @@ var domloader = {
}
);
},

html: function (dom, path) {
domloader.id++;
domloader.log('Include::HTML::' + path);
domloader.queue_async.push(
function () {
domloader.load(path);
$.get(domloader.root + path, {v: domloader.version}, function (response) {
$(dom).replaceWith(response);
}, 'text');
}
);
},

js: function (path) {
domloader.log('Include::JS::' + path);
Expand All @@ -84,7 +85,7 @@ var domloader = {
} else {
this.init_loading_page();
}
if (domloader.queue.length === 0) {
if (domloader.queue.length === 0 && domloader.queue_async.length === 0) {
if (domloader.queue_after.length !== 0) {
var cb = null;
for (cb in domloader.queue_after) {
Expand All @@ -95,6 +96,7 @@ var domloader = {
if (domloader.progressbar === false) {
this.autofix();
}

} else {
if (domloader.progressbar) {
domloader.total = domloader.queue.length;
Expand All @@ -104,7 +106,7 @@ var domloader = {
}
}
if (typeof (src) !== 'undefined') {
var percent = Math.ceil((this.total - this.queue.length) / this.total * 100);
var percent = Math.ceil((this.total - (this.queue.length+this.queue_async.length)) / this.total * 100);
if (domloader.animation) {
$('.domloader_curRate').animate({'width': percent + '%'}, this.animation_stime, function () {
if (percent === 100) {
Expand All @@ -123,6 +125,13 @@ var domloader = {

domloader.log("Loaded::" + src);
}
if (domloader.queue_async.length !== 0) {
var fn = domloader.queue_async.shift();
if (typeof (fn) === 'function') {
fn();
}
return true;
}
var fn = domloader.queue.shift();
if (typeof (fn) === 'function') {
fn();
Expand Down Expand Up @@ -164,8 +173,8 @@ var domloader = {
},

animation_slice: function () {
if (this.queue.length > 1) {
this.animation_stime = Math.ceil(this.animation_time / this.queue.length);
if ((this.queue.length+this.queue_async.length) > 1) {
this.animation_stime = Math.ceil(this.animation_time / (this.queue.length+this.queue_async.length));
} else {
this.animation_stime = this.animation_time;
}
Expand Down

0 comments on commit e55324b

Please sign in to comment.