Sachin Gupta Sachin Gupta - 29 days ago 5x
AngularJS Question

Calculate total time user has done activities on page like mousemove in jquery

Please help me. I have to find the total time user has done activities on page like mousemove, keypress etc. I want to get the useful time that page has been used.
I have made some of the code which calculates total time page has been opened and the time user has focused in that page.

An other approach to calculate this useful time can be by calculating the total idle time on page, for which nothing (i mean mousemove, keypress) has been done.
Useful time = Total Page opening time - total idle time

Please help
Here is the code.

var start, end, openingtime, pagefocustime = 0;
start =;

$(window).on('blur', function() {
end =;
pagefocustime += end - start

$(window).on('focus', function() {
start =;

end =;
pagefocustime += end - start
console.log("Exact Page Focused Time : ");
openingtime = end-start;
console.log("Total Page Read Time : "+ openingtime);

Please help.
Thanks in advance.


Here is the answer. Use this code with the above code.

function setup() {
		this.addEventListener("mousemove", resetTimer, false);
		this.addEventListener("mousedown", resetTimer, false);
		this.addEventListener("keypress", resetTimer, false);
		this.addEventListener("DOMMouseScroll", resetTimer, false);
		this.addEventListener("mousewheel", resetTimer, false);
		this.addEventListener("touchmove", resetTimer, false);
		this.addEventListener("MSPointerMove", resetTimer, false); 
	function startTimer() {
		timeoutID = window.setTimeout(goInactive, 10000);
	function resetTimer(e) {
	function goInactive() {
		idletime = idletime + inactivetime;
	function goActive() {