Ben Vishny wrote:

 // idle.js
 // Adapted from
// 
http://www.andrewsellick.com/67/simple-javascript-idle-state-using-//prototype
 // 15 min in ms
 var idleTime    = 900000;
 var timeOut     = ”;

 function init() {
     new Ajax.Request('/login/inactivity?do=check',
 {asynchronous:true, evalScripts:true});
     Event.observe(document.body, ‘mousemove’, resetIdle, true);
   Event.observe(document.body, ‘click’, resetIdle, true);
   Event.observe(document.body, ‘keypress’, resetIdle, true);

     setIdle();

 }

 function onIdleFunction(){

     new Ajax.Request('/login/logout?rsn=inactivity', 
{asynchronous:true,
 onComplete:function(){document.location.href='/login'}});

 }

 function resetIdle(){

     window.clearTimeout( timeOut );
     setIdle();

 }

 function setIdle(){

     timeOut = window.setTimeout( "onIdleFunction()", idleTime );

 }
 function unloadReport() {
   new Ajax.Request('/login/inactivity?do=set', {asynchronous:true});
 }
 Event.observe(window, ‘load’, init, false);
 Event.observe(window, ‘unload’, unloadReport, false);

EDIT: use this updated version of the code


# Goes in login controller

def inactivity
  case params[:do]
  when "check"
    # already done by check_activity before filter
  when "set"
    session[:inactive_at] = Time.now
  end
end

# application-wide before_filter
def check_activity
  if session[:inactive_at]
    if session[:inactive_at] < 15.minutes.ago
      reset_session
      flash[:notice] = "Your session has timed out due to inactivity."
      redirect_to :controller => :login
    else
      session[:inactive_at] = nil
    end
   end
end

The reason I use inactive_at rather than last_active is that the 
mouse/keyboard/click activity part would use up too many resources if it 
continually let the server know when stuff happened.
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to