如何自动(每天)更新javascript / ruby变量?(How can I update javascript/ruby variable automatically (everyday)?)
$(document).ready(function(e) {
$('#countdown').countdown({
until: date,
format: 'HMS',
labels: ['Years', 'Months', 'Weeks', 'Days', 'Hours', 'Minutes', 'Seconds'],
labels1: ['Year', 'Month', 'Week', 'Day', 'Hour', 'Minute', 'Second'],
onExpiry: function() {
$('#countdown').hide();
$('.reset-button').show();
}
});
});
我希望更新变量'date'(点击,按时间等)。 我怎样才能做到这一点?
这是一个application.js.erb文件。 这意味着JS和Ruby变量都受到欢迎。
$(document).ready(function(e) { $('#countdown').countdown({ until: date, format: 'HMS', labels: ['Years', 'Months', 'Weeks', 'Days', 'Hours', 'Minutes', 'Seconds'], labels1: ['Year', 'Month', 'Week', 'Day', 'Hour', 'Minute', 'Second'], onExpiry: function() { $('#countdown').hide(); $('.reset-button').show(); } }); });I would like to have the variable 'date' to be updated (on click, by time, etc). How can I do that?
This is a application.js.erb file. Which means JS and Ruby variables are both welcomed.
最满意答案
var date, year, month, day $(document).ready(function(e) { date = new Date(); if (date.getHours() > 22) { date.setDate(date.getDate()+1) } year = date.getFullYear(); month = date.getMonth(); day = date.getDate(); $('#countdown').countdown({ until: new Date(year, month, day, 22, 00, 00), format: 'HMS', labels: ['Years', 'Months', 'Weeks', 'Days', 'Hours', 'Minutes', 'Seconds'], labels1: ['Year', 'Month', 'Week', 'Day', 'Hour', 'Minute', 'Second'], onExpiry: function() { $('#countdown').hide(); $('.reset-button').show(); } }); });我找到了解决方案。 请注意,您必须多次使用JavaScript变量。 如果您使用Ruby,那么当浏览器刷新时它不会刷新,因为变量来自服务器(缓存)。
var date, year, month, day $(document).ready(function(e) { date = new Date(); if (date.getHours() > 22) { date.setDate(date.getDate()+1) } year = date.getFullYear(); month = date.getMonth(); day = date.getDate(); $('#countdown').countdown({ until: new Date(year, month, day, 22, 00, 00), format: 'HMS', labels: ['Years', 'Months', 'Weeks', 'Days', 'Hours', 'Minutes', 'Seconds'], labels1: ['Year', 'Month', 'Week', 'Day', 'Hour', 'Minute', 'Second'], onExpiry: function() { $('#countdown').hide(); $('.reset-button').show(); } }); });I have found the solution. Notice here that you HAVE TO use JavaScript variables for times. If you use Ruby, then it would not refresh when browser refreshes because the variables are from the server (cache).
更多推荐
发布评论