Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to log user activity. According other users response on StackOverflow I made a small jQuery library:

var stop_timeout = false;
$(window).blur(function(){
    elvis('left the building since ');
}).focus(function(){
    elvis('come back now:');
}).mousemove(function() {
    zima();
}).keyup(function() {
    zima();
});

function elvis(ce) {
    var now=new Date();
    $('.showMe').append('Elvis ' + ce + now.getHours() + ':' + now.getMinutes() +':'+now.getSeconds()+'<br>');
}

function zima() {
   clearTimeout(stop_timeout);
   stop_timeout = setTimeout(function() {
       $('.showMe').append('No activity since 10 seconds...<br>');
   }, 10000);
}

and html:

<input type="text" size="20"> 
<div class="showMe"></div>

The code is working Ok and, obviously, the mousemove and keyup events fires even I blur or focus on window. What I need is to fire mousemove and keyup ONLY on $(window).focus()

Thank you !

Edit: Also I made a Jfiddle here: http://jsfiddle.net/6RLBQ/5/

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

I think your keyup event should be trigger to your input element like,

$('input').keyup(function () {
    zima();
});

Full code

var stop_timeout = false;

$(document).blur(function () {
    elvis('left the building since ');
}).focus(function () {
    elvis('come back now:');
}).mousemove(function () {
    zima();
});

$('input').keyup(function () {
    zima();
});
share|improve this answer
    
Thank you Kumar, but is not about input element, because I want to construct some generic logging function, and not some particularized. Also it's about mousemove event. I try right now to set some var keys on window.focus() event to be true and check on mousemove and keyup... –  drLeo Jun 13 '13 at 12:04
add comment

Sorry for the stupid question, guys. I believe I get it now. mousemove() and keyup() is binding by default to $(window) and it's firing only on window focus. Question close !

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.