连续检查时间
首先,如何让 jQuery 连续检查时间,并在当前时间介于两个设定时间之间时向
标记添加 id
?我正在为一所学校制作一个时钟(其中时钟是通过电缆发送到每个房间的电视的网站),其中每个周期的表达如下:<div class="period 1">Period 1: 7:32-8:14</div>
我从 DataMapper,所以实际上,视图中的代码(一个 eRB 文件)是:
<% @periods.each do |@period| %>
<div class="period <%= @period.id %>"><%= @period.string %>: <%= @period.start_time.strftime("%I:%M") %>–<%= @period.end_time.strftime("%I:%M") %></div>
<% end %>
我的问题是如何让 jQuery 持续检查当前时间,如果它位于 @period.start_time
和 @period.end_time
之间。如果是这样,请将 id="active"
添加到
。我是否需要为 JavaScript 设置一些东西才能通过 .get
与 Ruby 交互?
如果您有任何疑问,请询问。
Firstly, how can I have jQuery continuously check the time, and add an id
to a <div>
tag if the current time is in between two set times? I'm making a clock for a school (where the clock is a website sent over cable to TVs in every room), where each period is expressed like this:
<div class="period 1">Period 1: 7:32-8:14</div>
I'm getting the start time and end time from DataMapper, so really, the code in the view (an eRB file) is:
<% @periods.each do |@period| %>
<div class="period <%= @period.id %>"><%= @period.string %>: <%= @period.start_time.strftime("%I:%M") %>–<%= @period.end_time.strftime("%I:%M") %></div>
<% end %>
My question is how I can get jQuery to continuously check the current time, and if it is in between @period.start_time
and @period.end_time
. If so, add id="active"
to the <div>
.
Would I have to set up something for JavaScript to interface with Ruby via .get
?
If you have any questions, please ask.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Javascript 的
Date()
对象来实现此目的。由于问题的澄清而进行的修改
CSS
Javascript
查看
实时示例的链接 - http://jsbin.com/okoso3
实例 2(修订版)- http://jsbin.com/okoso3/2
关于 Date() - https://developer.mozilla.org/en/JavaScript/参考/global_objects/日期
You can acheive this using Javascript's
Date()
object.Revision due to clarification of question
CSS
Javascript
Links to check out
Live Example - http://jsbin.com/okoso3
Live Example 2 (Revision) - http://jsbin.com/okoso3/2
About Date() - https://developer.mozilla.org/en/JavaScript/Reference/global_objects/date
您的 HTML 标记应该类似于这样:
您的 javascript 看起来像这样:
肯定有方法可以做得更好,但这样就可以了。
我会以自纪元以来的毫秒为单位传递时间,但我假设您将活动状态基于时间而不是日期和日期。时间。如果它基于日期和时间,您可以传递自纪元以来的毫秒数,而不是小时和分钟。
Your HTML markup should resemble this:
Your javascript will look like this:
There are definitely ways of doing this better, but this will do the job.
I would have passed in the time in milliseconds since epoch but I assume you are basing the active status on time rather than date & time. If it's based on date and time, you can pass in the milliseconds since epoch instead of hours and minutes.