# Rails "clock" and Javascript "clock" seconds apart

**URL:** <https://www.ruby-forum.com/t/rails-clock-and-javascript-clock-seconds-apart/141027>\
**Category:** Rails\
**Created:** [June 20, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/rails-clock-and-javascript-clock-seconds-apart/141027 "2008-06-20T21:44:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [June 20, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/rails-clock-and-javascript-clock-seconds-apart/141027/1 "2008-06-20T21:44:10Z")

</div>

Hi All,

I’m having a bit of an issue with time in my application. I’d be  
inclined to think that it was simply a machine time issue, but, as  
I’ve run the application on multiple machines, I’m not so sure.

Basically, my rails “current time” and my javascript “current time”  
are anywhere from 3 to 6 seconds apart. Is there any way for me to  
resolve this?

I suppose I could get a capture of each and add or subtract one from  
the other, but the trouble is that I’m using a timer, and the logic to  
ensure that the value doesn’t re-change after the initial setting  
could get out of hand. Are there perhaps any “static var” additions  
to Javascript?

Thanks in advance for any suggestions.

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [June 20, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/rails-clock-and-javascript-clock-seconds-apart/141027/2 "2008-06-20T22:07:52Z")

</div>

On Fri, 20 Jun 2008, gberz3 wrote:

> ensure that the value doesn’t re-change after the initial setting  
> could get out of hand. Are there perhaps any “static var” additions  
> to Javascript?

Do you really expect your browser and the remote server clocks to be  
exactly in sync?

–  
A

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [June 21, 2008, 3:17am UTC](https://www.ruby-forum.com/t/rails-clock-and-javascript-clock-seconds-apart/141027/3 "2008-06-21T03:17:36Z")

</div>

Yes, if they’re running on the same system. In a distributed system,  
not at all, but it would be nice if it were _consistently_ out of  
sync. Ya know? Still I need a mechanism to right the wrong.  
Thoughts?

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [June 22, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/rails-clock-and-javascript-clock-seconds-apart/141027/4 "2008-06-22T16:54:56Z")

</div>

On Jun 22, 5:34 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> between when you evaluated Time.now in your app and when the page  
> finished loading and the browser started to run the javascript?

No, the difference is actually between the Javascript “Time.now”, and  
the rails property “Object.created\_at”. What I did was just base the  
time on the server completely and worked some code like the following:

//SET SOME OTHER VALUES IN RAILS EARLIER

My initial problem was the seeming lack of Javascript “static” types.  
But using that allowed me to sort of create a static object. Anyway,  
basing the time strictly on the server worked.

One question: If you use RJS does it get re-evaluated on each  
iteration, or does it get “pumped out” once-for-all-time like regular  
Javascript?

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [June 22, 2008, 5:56pm UTC](https://www.ruby-forum.com/t/rails-clock-and-javascript-clock-seconds-apart/141027/5 "2008-06-22T17:56:27Z")

</div>

> One question: If you use RJS does it get re-evaluated on each  
> iteration, or does it get “pumped out” once-for-all-time like regular  
> Javascript?  
> evaluated on each request.

Fred

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [June 22, 2008, 11:34am UTC](https://www.ruby-forum.com/t/rails-clock-and-javascript-clock-seconds-apart/141027/6 "2008-06-22T11:34:51Z")

</div>

On 21 Jun 2008, at 02:16, gberz3 wrote:

> Yes, if they’re running on the same system. In a distributed system,  
> not at all, but it would be nice if it were _consistently_ out of  
> sync. Ya know? Still I need a mechanism to right the wrong.  
> Thoughts?

What exactly are you doing? Is the time difference just the difference  
between when you evaluated Time.now in your app and when the page  
finished loading and the browser started to run the javascript?
