0

I have this code to fade out some stuff on a page after you don't move your mouse for a second or so:

idleTime = 0;
var idleInterval = setInterval(function() { 
    idleTime++;
    if (idleTime > 1) {
        var isHovered = $('.fade-outs').is(":hover");
        if(isHovered == false) {
            $('.fade-outs').stop().fadeOut();
        }
    }
}, 1000);
$(document).bind('mousemove mousedown', function(e) {
    idleTime = 0;
    $('.fade-outs').fadeIn();
});

But, I am getting the following error with $('.fade-outs').is(":hover"); part:

Error: Syntax error, unrecognized expression: hover [http://localhost:5545/assets/js/jquery.min.js:3]

Does anyone know why I am getting this error?

6
  • 5
    There is no :hover in jQuery!
    – techfoobar
    Commented Sep 30, 2012 at 5:54
  • @techfoobar Oh, there isn't? The accepted answer on this is wrong then... stackoverflow.com/a/8981521/507629
    – Nathan
    Commented Sep 30, 2012 at 6:15
  • Actually, it appears to be correct. The jsFiddle is working with :hover.
    – Nathan
    Commented Sep 30, 2012 at 6:17
  • The accepted answer is correct. It uses a class named hover and the .hover (with the dot) selector which is correct.
    – techfoobar
    Commented Sep 30, 2012 at 6:20
  • The accepted answer here is using a colon (:hover). Take a look at the jsFiddle example in the answer.
    – Nathan
    Commented Sep 30, 2012 at 6:22

2 Answers 2

3

I would try something like this instead:

var idleTimer;

function startTimer() {
    stopTimer();

    idleTimer = setInterval(function() {
        $('.fade-outs').stop().fadeOut();
    }, 1000);
}

function stopTimer() {
    idleTimer && clearInterval(idleTimer);
}

$(document).on('mousemove', startTimer);

$('.fade-outs').on({
    mouseleave: startTimer,
    mouseenter: stopTimer,
    mousemove: function(e) {
        e.stopPropagation();
    }
});​

Demo: http://jsfiddle.net/Blender/urzug/4/

4
  • Hi Blender, would you be kind enough to explain this line for me idleTimer && clearInterval(idleTimer); within the stopTimer function. Is this a shorthand I've been missing out on in JS? Commented Sep 30, 2012 at 6:19
  • In JS, the && operator short-circuits, which means that if the left-hand-side is false, the right-hand-side won't be evaluated at all (either way the resulting value will be false). If idleTimer doesn't have a value, idleTimer is not truthy so the operator short-circuits and clearInterval is not called. If idleTimer is defined, clearInterval is called. a && b is the same as a ? a : b (if you know about ternary operators).
    – Blender
    Commented Sep 30, 2012 at 6:23
  • I do indeed but I've not seen them written like this before. It makes perfect sense thinking about it as you are calling a function in a condition and therefore no need for true/false variable assignment... thank you for the explanation, this will help me. Commented Sep 30, 2012 at 6:24
  • Thanks for the answer, I decided to go with the other one but this one is good as well so I have up voted it. Nice shorthand trick in JS with the && operator - never knew about it!
    – Nathan
    Commented Sep 30, 2012 at 6:28
1

Try changing it to this:

idleTime = 0;
var idleInterval = setInterval(function() { 
idleTime++;
if (idleTime > 1) {
    var isHovered = $('.fade-outs').is(".hover");
    if(isHovered == false) {
        $('.fade-outs').stop().fadeOut();
    }
}
}, 1000);
$(document).bind('mousemove mousedown', function(e){
  idleTime = 0;
  $('.fade-outs').fadeIn();
});
$('.fade-outs').hover(funciton() { $(this).toggleClass('hover') });
0

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