我正在用JavaScript和Tabletop.js制作一个随机引号生成器。当我的页面加载时,将自动初始化从我的电子表格中提取引用和生成的函数。我不希望它生成一个新的引号,除非页面/区域被点击。
顺便说一句,这是一个粗俗的页面,基于greatfu**ingstartupadvice.com
以下是代码页:
http://codepen.io/JonLangel/pen/kXdmqP?editors=1010
以下是Codepen页面中的JS:
var my_spreadsheet = 'https://docs.google.com/spreadsheets/d/1sX822Oiga0j0eHC8PvegRo6W8GGC0a28nVgnvSsOqo/pubhtml';
var sheet_data;
function init() {
Tabletop.init( {
key: my_spreadsheet,
callback: saveData,
simpleSheet: true
});
}
function saveData(data, tabletop) {
sheet_data = data;
newQuote();
}
function newQuote() {
var randNum = Math.floor(Math.random() * (sheet_data.length));
$("#advice").html(sheet_data[randNum].Advice).addClass('animated zoomIn').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
$(this).removeClass();
});
}
$(document).ready(function() {
init();
$('.container').onclick(function(){
newQuote();
});
});发布于 2016-08-25 15:39:14
在页面的开头,您正在初始化Tabletop,但是在init的回调中:
callback: saveData //saveData is callback of init并编写此函数:
function saveData(data, tabletop) {
sheet_data = data;
newQuote(); //here You have newQuote called
}从newQuote() saveData中删除,并在页面加载时不会生成引号
function saveData(data, tabletop) {
sheet_data = data;
}发布于 2016-08-25 15:37:12
调用saveData作为Tabletop.init andsaveData的回调调用newQuote
您应该简单地删除对newQuote的saveData调用
发布于 2016-08-25 15:39:12
在回调中调用newQuote()。
从newQuote函数中删除saveData ()。
https://stackoverflow.com/questions/39149315
复制相似问题