【发布时间】:2016-12-19 01:13:34
【问题描述】:
我正在用 Jekyll 建立一个网站。很多人会贡献不同的 javascript 草图,这些草图会随机显示在主页上:其中一些是用 P5.js 编写的,另一些是用其他库编写的。
由于我不想在页面加载时加载所有库,因此我生成了一个 JSON,每个脚本只有一个对象:
[
{
"title":"Title to display",
"author":"Author",
"description":"javascript, Processing",
"kind":"javascript",
"href":"link to the js hosted on GitHub Pages",
"includes":[
"link to external library hosted on CDN",
"link to external library hosted on CDN"
]
}
]
然后,使用 jQuery,我将使用此脚本加载所有内容:
var loadedData;
$(function() {
// Load all the scripts in an array
$.getJSON("{{ site.url }}/homepage.json", function(data) {
loadedData = data;
randomPost();
});
$("#jsRefresh").click(function() {
$("canvas").remove();
randomPost();
});
});
function randomPost() {
$("#loading").show();
item = loadedData[Math.floor(Math.random() * loadedData.length)];
var defer = $.Deferred();
$.each(item.includes, function(index, value){
defer.then(function() {
$.getScript(value)
});
});
defer.then(function() {
$.getScript(item.href)
});
defer.resolve();
$.when(defer).done(function() {
$("#loading").hide();
$(".item-title").html(item.title + ' | ' + item.author);
$(".item-description").html(item.description);
})
}
此代码在我的本地服务器上运行,但是当我将其上传到 GitHub Pages 时,它在页面加载时无法正确加载。
如果我再次点击触发 randomPost() 的刷新链接,它会加载上一个脚本。
现场示例目前托管在here
【问题讨论】:
-
getScript() 在哪里???
标签: javascript jquery github-pages p5.js