Skip to content

Commit

Permalink
feat(load): add a load queue
Browse files Browse the repository at this point in the history
load a javascript load queue
  • Loading branch information
cubesky committed Jan 19, 2017
1 parent 943ca30 commit 4cd105e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 23 deletions.
7 changes: 0 additions & 7 deletions layout/_partial/config_css.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@
background-color: #fff;
}
</style>
<script type="text/javascript">
window.onload = function () {
setTimeout(function () {
document.body.background="https://api.i-meto.com/bing?<%= theme.background.bing.parameter %>";
}, 2000);
}
</script>
<% } else if(!theme.background.bgimg) { %>
<style>
body{
Expand Down
26 changes: 14 additions & 12 deletions layout/_partial/footer-option.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
<% if(theme.comment.use == "duoshuo") { %>
<!-- 多说公共 js 代码 start -->
<script type="text/javascript">
var duoshuoQuery = {
short_name: '<%= theme.comment.shortname %>'
};
(function() {
var ds = document.createElement('script');
ds.type = 'text/javascript';
ds.async = true;
ds.src = '<%= theme.comment.duoshuo_embed_js_url %>';
ds.charset = 'UTF-8';
(document.getElementsByTagName('head')[0]
|| document.getElementsByTagName('body')[0]).appendChild(ds);
})();
queue.offer(function(){
var duoshuoQuery = {
short_name: '<%= theme.comment.shortname %>'
};
(function() {
var ds = document.createElement('script');
ds.type = 'text/javascript';
ds.async = true;
ds.src = '<%= theme.comment.duoshuo_embed_js_url %>';
ds.charset = 'UTF-8';
(document.getElementsByTagName('head')[0]
|| document.getElementsByTagName('body')[0]).appendChild(ds);
})();
});
</script>
<!-- 多说公共 js 代码 end -->
<% } %>
Expand Down
8 changes: 8 additions & 0 deletions layout/_partial/head.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<%- css("css/style.min") %>
<%- partial("_partial/config_css") %>
<%- js("js/jquery.min") %>
<%- js("js/queue") %>

<%- css("css/highlight/" + theme.reading.code_highlight ) %>

Expand Down Expand Up @@ -119,6 +120,13 @@
</script>
<% } %>

<!-- Bing Background -->
<% if(theme.background.bing.enable) { %>
<script type="text/javascript">
queue.offer(function(){document.body.background="https://api.i-meto.com/bing?<%= theme.background.bing.parameter %>"});
</script>
<% } %>

<!-- Custom Head -->
<% if (site.data.head) { %>
<% for (var i in site.data.head) { %>
Expand Down
6 changes: 2 additions & 4 deletions layout/_widget/disqus.ejs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<div id="disqus_thread"></div>
<script>
$('html').ready(function() {
setTimeout(function() {
queue.offer(function() {
var disqus_config = function () {
this.page.url = '<%- config.url + url_for(path) %>'; // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
Expand All @@ -14,6 +13,5 @@
s.setAttribute('data-timestamp', + new Date());
(d.head || d.body).appendChild(s);
})();
},1500);
});
});
</script>
45 changes: 45 additions & 0 deletions source/js/queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function Queue() {
this.dataStore = [];
this.offer = offer;
this.poll = poll;
this.execNext = execNext;
this.debug = false;
this.startDebug = startDebug;


function offer(element) {
if(this.debug) console.log("Offered a Queued Function.");
if(typeof element === "function") {
this.dataStore.push(element);
} else {
console.log("You must offer a function.");
}
}

function poll() {
if(this.debug) console.log("Polled a Queued Function.");
return this.dataStore.shift();
}

function execNext() {
var nextfunc=this.poll();
if(nextfunc != undefined) {
if(this.debug) console.log("Run a Queued Function.");
nextfunc();
}
}

function startDebug(){
this.debug = true;
}

}

var queue = new Queue();
$(document).ready(function(){
setTimeout(function(){
setInterval(function(){
queue.execNext();
},500);
},3000);
});

0 comments on commit 4cd105e

Please sign in to comment.