Skip to content Skip to sidebar Skip to footer

Detect Mousemovent Inside Of Blur Function Jquery

I am trying to create a neat way to stop an AJAX called based upon if the browser is in focus, and if the mouse moves.. So here's what I want it to do: If the user goes to a differ

Solution 1:

http://jsfiddle.net/popnoodles/5mqMm/

You probably want this using .one(). This will see the mouse move, run your procedure and not run it again, until the window is reloaded or it's on another page.

Putting it inside of blur means blurring sets it up again.

}).blur(function () {
    $(document).one('mousemove', function(){
       // i react ONCE to the mouse being moved
         console.log('Reinitialize');
         clearTimeout(st);
         waitForMsg();
         // focus the window again as desired
         $(window).trigger('focus');
    });
    if (!window_focus) {
        return
    }
    console.log('Init Suspension...');
    // Set Timeout
    $(function () {
        st = setTimeout(function () {
            clearTimeout(setTimeoutConst);
            window_focus = false;
            document.title = 'Timed Out | WEBSITE';
            console.log('Suspended');
        }, 60000);    
    });
});

Solution 2:

Try this jsfiddle

var window_focus = true, lastMouseMoveTime;
$(document).ready(function () {    
    lastMouseMoveTime = new Date().getTime();
    $('#alertbox').click(function () {
        $('#alertbox').slideUp("slow");
    });

    // Check focal point
    $(window).focus(function () {
        if (window_focus) {
            return
        }
        window_focus = true;
        waitForMsg();

    }).blur(function () {
        if (!window_focus) {
            return
        }
        window_focus = false;
        console.log('Init Suspension...');
        // Set Timeout
    });

    waitForMsg();
    $(document).mousemove(function(){
        lastMouseMoveTime = new Date().getTime();
        if(!window_focus ){
            waitForMsg(); // restarting ajax if it stopped because of mousemove.
        }

    });

});

in your ajax call method

if( !window_focus){
     if( new Date().getTime() - lastMouseMoveTime   > 60*1000 ){
            return;
     }
}

Post a Comment for "Detect Mousemovent Inside Of Blur Function Jquery"