# How to show a Timer for lock expiry when editing a record

**URL:** <https://www.ruby-forum.com/t/how-to-show-a-timer-for-lock-expiry-when-editing-a-record/189657>\
**Category:** Rails\
**Created:** [June 4, 2010, 3:51pm UTC](https://www.ruby-forum.com/t/how-to-show-a-timer-for-lock-expiry-when-editing-a-record/189657 "2010-06-04T15:51:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vijay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4bbf92/32.png) [@vijay](https://www.ruby-forum.com/u/vijay)\
**Post date:** [June 4, 2010, 3:51pm UTC](https://www.ruby-forum.com/t/how-to-show-a-timer-for-lock-expiry-when-editing-a-record/189657/1 "2010-06-04T15:51:28Z")

</div>

I’m newbie to ruby on rails.  
In my application, i was trying to show the remaining time the user is  
allowed to edit an instance record( like milestones, cheklists etc.).  
I was locking it from multiple users to edit it at the sametime.  
When the user clicks the edit link\_to i need to display the timer in  
the edit.rhtml

I have the javascript function for timer updation. when called it  
updates the text\_field with the time.

function update\_timer() {  
var page\_loaded\_ms = new Date().getTime();  
var timeout\_ms = 100\*1000;  
var time\_left = Math.round((timeout\_ms - new Date().getTime()

- page\_loaded\_ms) / 1000);  
var minutes = Math.floor(time\_left/60);  
var seconds = time\_left - (minutes\*60);  
$(‘time\_left\_value’).value = ‘Time left: ‘+ minutes + ’ min’ +  
zeroPad(seconds, 2)+ ’ sec’;  
setTimeout(“update\_timer()”, 1000);  
}

function zeroPad(number, width) {  
var s = number.toString();  
while (s.length \< width)  
s = “0” + s;  
return s;  
}

could anyone suggest me a solution for this.

---

<div class="post-metadata">

**Author:** ![vijay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4bbf92/32.png) [@vijay](https://www.ruby-forum.com/u/vijay)\
**Post date:** [June 4, 2010, 8:03pm UTC](https://www.ruby-forum.com/t/how-to-show-a-timer-for-lock-expiry-when-editing-a-record/189657/2 "2010-06-04T20:03:10Z")

</div>

Vijay wrote:

> I’m newbie to ruby on rails.  
> In my application, i was trying to show the remaining time the user is  
> allowed to edit an instance record( like milestones, cheklists etc.).  
> I was locking it from multiple users to edit it at the sametime.  
> When the user clicks the edit link\_to i need to display the timer in  
> the edit.rhtml

I thought for a second about attempting to answer this, but quickly  
realized. DO NOT DO THIS!

Pessimistic locking on databases is very, very bad form. Do not attempt  
to prevent concurrent editing. You’re just asking for trouble. Users of  
modern applications will not accept pessimistic style record locking.

Instead, develop a recovery solution based on an optimistic locking  
scheme. ActiveRecord has some built-in support for optimistic locking,  
which will get you started. If the optimistic locking mechanism detects  
concurrent edits then present that to the user with enough information  
to recover.

P.S. Using \*.rhtml extensions is so one-dot-ex. If you’re using a  
version of Rails 2.0 or higher, switch your views to use \*.html.erb.
