Run action after 2 minutes without moving the mouse

Asked

Viewed 2,834 times

6

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

Example: The user is on the page php products. and if the user is stopped with the mouse or it is in another tab/tab (Facebook for example), after 2 minutes he redirect for aguarde.php.

3 answers

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

  • If I run this code and keep moving the mouse right from the start for more than 3 seconds and then stop and leave the mouse stopped it does not run. It only runs if when I start the script, get the mouse stopped

  • And also if I have the mouse on top of the document it does not redirect, only gets out of the document

  • Show your code, you can start the timeout and only in Mousemove give a clear on it

  • I edited it, test it

  • Sorry to be resurrecting the topic, but I need exactly this on my system. Does this code only work in the case of the mouse? and in the case of the keyboard? because many people may be in chat for example.

  • @Jose.Marcos yes, see that the event is in "Mousemove", if it is by keyboard, change it to keyup

  • Right Rod, I’d actually like to for both of us. Like I would in this case?

Show 2 more comments

8


Code adapted from @Rod

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

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

In my example was added the function window.location.href that creates the redirecting and was removed the function that caused when passing the mouse over the page excludes the content of the page.

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

After 2 minutes is redirected to http://answall.com

Now just edit it with the URL to which you want the redirect to occur. In this case:

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

Now is 100% functional, just the way you wanted.

Demontraction

Fiddle’s link

Obs: In the examples created in jsFiddle was given only 3,000 milliseconds = 3 seconds, not having to wait 2 minutes to see how it works.

1

Unused 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.
})()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.