Monday, March 28, 2011

How to prompt user if page is idle from last 5 minutes

While going through a interview, a interviewer asked me this question which made me to think over it.
The question was:

I have a web page and i want that, if a user not perform any operation (enter text or select a item from dropdownList or any other such) for last 5 minutes on that page then he should be prompted by a message.

i answered the question but he was not looking satisfied. Later when i worked over it and got to know that my answer was correct but its was not optimized.

so below is an optimized solution for such problem:

$(window).bind( 'mousemove mouseclick keydown mousewheel ...more listeners...', idle );
function idle( e )
{
  if ( window.idleTimeout )
  {
    clearTimeout( window.idleTimeout );
  }
  window.idleTimeout = setTimeout( userIdle, 300000 );
}
function userIdle()
{
  //either do stuff here or trigger the "idle" event
  $(window).trigger('idle');
}


in above example we are using jquery to binding all required event listeners with a timer to trigger our function which will prompt user that your are idle on page form last 5 minutes.

For more ASP.NET interview questions click here

Solution By: Rajesh Rolen

Share This!


5 comments:

Anonymous said...

Wow, great solution.

J R Bishnoi said...

wow sir best solution.

J R Bishnoi said...

wow sir best solution.

J R Bishnoi said...

great solution sir ji.

Dr. Rajesh Rolen said...

Thanks Raj, such comments makes me energetic to write.

Powered By Blogger · Designed By Seo Blogger Templates