Redirect page if user does not interact (click, touchstart, mousemove)

0

I need to make a redirect to a "sleep screen", in case the user does not interact via click / touch, drag and mouse movement.

What you can do

<script type="text/javascript">

    var meuTempo;

    meuTempo = setTimeout(openUrl, 5000); // redirecionamento

    function resetTimer(){
    clearTimeout(meuTempo); // dá um clear no tempo para dar reload
    }

    function openUrl(){
        window.location = "https://www.google.com.br/"; // reload
    }

    jQuery(document).bind("click mousemove touchstart", resetTimer); // reset de timer por interação

</script>

The problem

It is necessary that whenever the user does not interact, the counter is set again.

That is, if the user interacts, he stays on the page, if not, he is redirected after X seconds to another screen.

    
asked by anonymous 12.01.2018 / 18:42

1 answer

0

Use a function to count the seconds, and use the bind below to test user interactions:

$(document).on("click keydown keyup mousemove")

var segundos = 0;
var timer = setInterval(testarInteracao, 1000); //1000 ms = 1 segundo

function testarInteracao(){
    segundos+= 1;
    
    if(segundos == 10){ //Limite máximo em Segundos
        window.location.href = "www.google.com.br";
    } 
    
    console.log(segundos)
}

$(document).on("click keydown keyup mousemove", function(){
    segundos = 0;
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

If you need other events , place them in the same bind of% space_with%

    
12.01.2018 / 18:49