连续检查时间

发布于 2024-09-25 07:47:33 字数 906 浏览 3 评论 0原文

首先,如何让 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") %>&ndash;<%= @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏天碎花小短裙 2024-10-02 07:47:33

您可以使用 Javascript 的 Date() 对象来实现此目的。
由于问题的澄清而进行的修改

CSS

.active { font-weight: bold; } // Or whatever styling you need

Javascript

var loop = setInterval(function() {
  var d = new Date();
  var dh = d.getHours();
  var cP = 16; 
  /* make cP your earliest period
  earlier than 1600, but that's for it to
  work with the current time for me (8pm) 
  each pass with increment by 1 hour.*/
  $('.period').each(function() {
    if (dh==cP) $(this).addClass('active'); // add active if current
    else $(this).removeClass('active'); // otherwise remove it
    cP++; // Add an hour each loop, assuming each period is 1hr
  });
}, 60000); // 60000 milliseconds = 1 minute update

查看

实时示例的链接 - 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

.active { font-weight: bold; } // Or whatever styling you need

Javascript

var loop = setInterval(function() {
  var d = new Date();
  var dh = d.getHours();
  var cP = 16; 
  /* make cP your earliest period
  earlier than 1600, but that's for it to
  work with the current time for me (8pm) 
  each pass with increment by 1 hour.*/
  $('.period').each(function() {
    if (dh==cP) $(this).addClass('active'); // add active if current
    else $(this).removeClass('active'); // otherwise remove it
    cP++; // Add an hour each loop, assuming each period is 1hr
  });
}, 60000); // 60000 milliseconds = 1 minute update

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

冷夜 2024-10-02 07:47:33

您的 HTML 标记应该类似于这样:

<div class="period 1">
  <input type="hidden" name="time" value="7" class="start_time_hour" />
  <input type="hidden" name="time" value="32" class="start_time_min" />
  <input type="hidden" name="time" value="8" class="end_time_hour" />
  <input type="hidden" name="time" value="14" class="end_time_min" />
  Period 1: 7:32-8:14</div>
</div>

您的 javascript 看起来像这样:

var elements = $('.period');
function update_status() {
  elements.each(function() {
    var current = new Date();
    var sH = parseInt($(this).find('.start_time_hour').val());
    var sM = parseInt($(this).find('.start_time_min').val());
    var eH = parseInt($(this).find('.end_time_hour').val());
    var eM = parseInt($(this).find('.end_time_min').val());

    if (current.getHours() >= sH && current.getHours() <= eH && current.getMinutes() >= sM && current.getMinutes() <= eM) {
      $(this).setAttr('id', 'active');
    }
  });
}
setInterval(update_status, 1000);

肯定有方法可以做得更好,但这样就可以了。
我会以自纪元以​​来的毫秒为单位传递时间,但我假设您将活动状态基于时间而不是日期和日期。时间。如果它基于日期和时间,您可以传递自纪元以来的毫秒数,而不是小时和分钟。

Your HTML markup should resemble this:

<div class="period 1">
  <input type="hidden" name="time" value="7" class="start_time_hour" />
  <input type="hidden" name="time" value="32" class="start_time_min" />
  <input type="hidden" name="time" value="8" class="end_time_hour" />
  <input type="hidden" name="time" value="14" class="end_time_min" />
  Period 1: 7:32-8:14</div>
</div>

Your javascript will look like this:

var elements = $('.period');
function update_status() {
  elements.each(function() {
    var current = new Date();
    var sH = parseInt($(this).find('.start_time_hour').val());
    var sM = parseInt($(this).find('.start_time_min').val());
    var eH = parseInt($(this).find('.end_time_hour').val());
    var eM = parseInt($(this).find('.end_time_min').val());

    if (current.getHours() >= sH && current.getHours() <= eH && current.getMinutes() >= sM && current.getMinutes() <= eM) {
      $(this).setAttr('id', 'active');
    }
  });
}
setInterval(update_status, 1000);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文