Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1096127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:14:07+00:00 2026-05-17T00:14:07+00:00

Firstly, how can I have jQuery continuously check the time, and add an id

  • 0

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") %>&ndash;<%= @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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:14:07+00:00Added an answer on May 17, 2026 at 12:14 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly, there have some tag links in my main page. click each one, post
From: http://doc.qt.nokia.com/4.7/signalsandslots.html Callbacks have two fundamental flaws: Firstly, they are not type-safe. We can
Two questions really... Firstly, which is the better coding style when writing jQuery functions
Firstly, I have defined a variable count=10 and made it readonly. Now, how can
Firstly: I am totally a newbie for this kind of work. I have a
Firstly, I do not have any malicious intent out of this question. I would
Firstly, Modifying may be the wrong term, I see a few people have posted
i have been developing a URL shortener and would now like to add the
I have become interested in jQuery and jQuery UI libraries for the benefits they
Firstly, I have read these: (1) http://www.gaiagps.com/news/article/iOS5%20Breaks%20Data%20Storage%20and%20Crushes%20My%20Soul (2) http://iphoneincubator.com/blog/data-management/local-file-storage-in-ios-5 (I can not submit more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.