Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make function work when the mouse not moving?

$(document).ready(function() {
    var score = 0;

    $("body").mousemove(function() {        
        score++;
        $("#result").val(score);
        console.log(score);
    });

 });

The score will increase every time when I move the mouse, but how should I add a function to decrease the score until 0 when the mouse is not moving?

like image 844
Roger Liao Avatar asked May 29 '15 00:05

Roger Liao


People also ask

What to do if the mouse cursor is not moving?

Check that the battery of the mouse is charged. Make sure that the receiver (dongle) is firmly plugged in to the computer. If your mouse and receiver can operate on different radio channels, make sure that they are both set to the same channel.

How do I enable mouse function?

To access mouse settings, select the Start button, then select Settings > Ease of Access > Mouse . Turn on the toggle under Control your mouse with a keypad if you want to control your mouse using a numeric keypad. Select Change other mouse options to change your primary mouse button, set scrolling options, and more.


1 Answers

You could set an interval that decreases the value if the mouse does not move, and clear it when it moves, and reset it, something like this:

$(document).ready(function() {
    var score = 0, decreaseInterval, intervalTime = 1000;

    function decrease() {
        if (score > 0) score--;
        $("#result").val(score);
    };

    decreaseInterval = setInterval(decrease, intervalTime);

    $("body").mousemove(function(){
        clearInterval(decreaseInterval);
        score ++;
        $("#result").val(score);
        decreaseInterval = setInterval(decrease, intervalTime);
        console.log(score);
    });
});

Here is a fiddle to demonstrate it working: https://jsfiddle.net/0swrae76/1/

like image 170
taxicala Avatar answered Sep 19 '22 06:09

taxicala