如何自动(每天)更新javascript / ruby变量?(How can I update javascript/ruby variable automatically (everyday)?)

编程入门 行业动态 更新时间:2024-10-13 16:16:00
如何自动(每天)更新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).

更多推荐

本文发布于:2023-08-03 22:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   ruby   javascript   update   everyday

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!