Execute action after 2 minutes without moving the mouse

5

How to create a function in jQuery that redirects the visitor to a page after it does not move the mouse for 2 minutes or more?

Example: The user is on the products.php page and if the user is left standing with the mouse or he is on another tab (Facebook for example) after 2 minutes it redirects to aguarde.php .

    
asked by anonymous 22.07.2014 / 00:44

3 answers

7

@Rod adapted code

$(function() {
    timeout = setTimeout(function() {
        window.location.href = "http://pt.stackoverflow.com";
    }, 120000);
});

$(document).on('mousemove', function() {
    if (timeout !== null) { 
        clearTimeout(timeout);
    }
    timeout = setTimeout(function() {
        window.location.href = "http://pt.stackoverflow.com";
    }, 120000);
});

In my example the redirect function was added and was removed from the function that caused the mouse to move over the is deleted from the page content.

A count in milliseconds that is equivalent to 2 minutes ( 120,000 milliseconds is equal to 120 seconds ), which is equal to 2 minutes , because 1 minute has 60 seconds ).

After 2 minutes you are redirected to link

Now you only need to edit it with the window.location.href to which you want redirection to occur. In case:

window.location.href = "aguarde.php";

Now it's 100% functional , just the way you wanted it to be.

Demonstration

Fiddle Link

In the examples created in jsFiddle was given only 3,000 milliseconds = 3 seconds, so you do not have to wait 2 minutes to see the operation.

    

22.07.2014 / 04:42
9

You can use a timer in an event mousemove

As in the example below:

 $(function() { //onload
   setEvent();

});

$(document).on('mousemove', function() { //mouse move 
    if (timeout !== null) {
        $(document.body).text('');
        clearTimeout(timeout); //clear no timer
    }
    setEvent(); //seta ele novamente para caso aja inatividade faça o evento
});

function setEvent(){
    timeout = setTimeout(function() {
        $(document.body).text('Mouse idle for 3 sec'); //Teu evento após terminar o timer
    }, 3000); //tempo do timer
}

DEMO

Reference: Link

    
22.07.2014 / 00:49
0

Without using add-ons , you can make a block of commands like this:

(function()
{
    var mouse = {};
    // mouse.timer --> tempo para ação
    // mouse.moved --> callback quando o mouse é movido
    // mouse.action --> sua ação de 2 minutos
    mouse.action = function ()
    {
        // 2 minutos. Bye!
    };
    mouse.moved = function ()
    {
        if(mouse.timer)
            // O tempo já estava definido.
            clearTimeout(mouse.timer); // Então para.
        mouse.timer = setTimeout(mouse.action, 120000)
    };
    addEventListener("mousedown", mouse.moved); // ao segurar ou clicar
    addEventListener("mousemove", mouse.moved); // ao mover
    addEventListener("touchstart", mouse.moved); // ao tocar
    addEventListener("load", function ()
    {
        mouse.timer = setTimeout(mouse.action, 120000)
        // Já execute o tempo para ação quando a página carregar.
    });
    // Você pode acrescentar:
    // addEventListener("keydown", mouse.moved)
    // se quiser que o mesmo aconteça ao pressionar uma tecla.
})()
    
20.04.2016 / 22:01