# Time.now "stuck" in Javascript Function

**URL:** <https://www.ruby-forum.com/t/time-now-stuck-in-javascript-function/178636>\
**Category:** Rails\
**Created:** [November 8, 2009, 4:55pm UTC](https://www.ruby-forum.com/t/time-now-stuck-in-javascript-function/178636 "2009-11-08T16:55:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 8, 2009, 4:55pm UTC](https://www.ruby-forum.com/t/time-now-stuck-in-javascript-function/178636/1 "2009-11-08T16:55:36Z")

</div>

Novice RoR programmer here, I’ve run into a bit of a stumbling block,  
concerning Time.now seemingly not updating in between JS function  
calls.

My code is as follows:

function test()  
{  
document.getElementById(“chatbox”).value += “\<%= Time.now %\>” + “\n”;  
}

…

Basically, clicking the textarea “chatbox” will add a line with the  
current time, and unfortunately, every click on the textarea results  
in the identical line being written, which is the time that the page  
was loaded.

Is there any way I could get Time.now to update with every click?

Thanks in advance!

Nick.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 8, 2009, 4:58pm UTC](https://www.ruby-forum.com/t/time-now-stuck-in-javascript-function/178636/2 "2009-11-08T16:58:32Z")

</div>

On Nov 8, 3:36 pm, Nick S [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Basically, clicking the textarea “chatbox” will add a line with the  
> current time, and unfortunately, every click on the textarea results  
> in the identical line being written, which is the time that the page  
> was loaded.

That’s completely normal - At the point that the template is rendered  
all of your \<%= are evaluated - if you look at the source in the  
browser you won’t see any of your ruby code (and even if you could,  
what’s your browser supposed to do with ruby). If you need some ruby  
code evaluated you need to make a request to the server

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 8, 2009, 5:02pm UTC](https://www.ruby-forum.com/t/time-now-stuck-in-javascript-function/178636/3 "2009-11-08T17:02:02Z")

</div>

Ahh I see, thank you very much for your help Frederick!

On Nov 9, 12:57 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 8, 2009, 6:17pm UTC](https://www.ruby-forum.com/t/time-now-stuck-in-javascript-function/178636/4 "2009-11-08T18:17:25Z")

</div>

Frederick C. wrote:

> On Nov 8, 3:36ï¿½pm, Nick S [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Basically, clicking the textarea “chatbox” will add a line with the  
> > current time, and unfortunately, every click on the textarea results  
> > in the identical line being written, which is the time that the page  
> > was loaded.
> 
> That’s completely normal - At the point that the template is rendered  
> all of your \<%= are evaluated - if you look at the source in the  
> browser you won’t see any of your ruby code (and even if you could,  
> what’s your browser supposed to do with ruby). If you need some ruby  
> code evaluated you need to make a request to the server

But in this case, you probably don’t need a server request – I think  
JavaScript has an equivalent to Time.now.

In any case, I would advise against using ERb in JavaScript…

> Fred

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
