Run two functions in setTimeout

0

Very good, I have this code in which I intend that after inactivity, redirected to login.html

var nameTime;
function ini() {
    nameTime = setTimeout('location="login.html"',5000); 
 }
function parar() {
  clearTimeout(nameTime);
    nameTime = setTimeout('location="login.html"',5000); 
 }

After

<body onload="ini()" onkeypress="parar()" onclick="parar()" onmousemove="parar()" >

I would like to put some kind of warning that has been redirected by inactivity, something toast or alert. I already have this, what I do not know is how to make the user see some type of message when it is redirected.

    
asked by Lorenzo Martín 17.01.2018 в 09:59
source

1 answer

1

What you should do is that the setTimeout call a function that displays the message and, with another timeout to give the user time to read it, redirect to the login screen.

Something like this:

var nameTime;
function ini() {
  nameTime = setTimeout(redirigir, 5000); 
}
function parar() {
  clearTimeout(nameTime);
  nameTime = setTimeout(redirigir, 5000); 
}
function redirigir(){
  document.getElementById('message').style.display='inline';
  setTimeout('location="login.html"',1500); 
}
body{
  padding: 10px;
}
#message{
  font-size: 20px;
  font-weight: bold;
  border: solid 1px #333333;
  padding: 15px;
  display:none;
}
<body onload="ini()" onkeypress="parar()" onclick="parar()" onmousemove="parar()" >
<div id="message">
  Va a ser redirigido a la pantalla de login por inactividad
</div>
    
answered by 17.01.2018 / 10:14
source