0

I need to track the total time the student spend viewing his course pages, after logging, I am using devise gem.

I can store the DateTime.Now when the student starts watching a course, but, I have the following challenges to beat:

  1. the student might open another window tab, leaving the course webpage, how can I pause time tracking while the student not viewing the course webpage, and resume tracking when he return?

  2. to recognizing the student being idle for a certain time ? ( doing nothing or even minimize the browser )

Any hint or help will be more than appreciated, is there a specific gem or jQuery plugin that can make the task easier ? or a recommend approach ?

simo
  • 23,342
  • 38
  • 121
  • 218
  • 2
    possible duplicate of [How to measure a time spent on a page?](http://stackoverflow.com/questions/4667068/how-to-measure-a-time-spent-on-a-page) – Benoit Garret Oct 24 '11 at 15:45

1 Answers1

1

For the first issue you should take a look at onFocus and onBlur events.

window.onblur = function() {
  stopWatching();
};

window.onfocus = function() {
  startWatching();
};

For the second, i can't really help you but : you cant play with javascript events like mousemove or keypress.

http://api.jquery.com/category/events/

iRyusa
  • 384
  • 2
  • 13
  • Thanks, I found a jQuery for checking mouse move or click, very nice, its here: http://paulirish.com/demo/idle-timer it even works when the window is not focused, but, still, I need a way to tell the rails to close session or do something – simo Oct 26 '11 at 07:31
  • Cool plugin :) thanks for sharing. See the link of Benoit Garret you can send a request before unload with : $(window).unload(function() { – iRyusa Oct 26 '11 at 08:13