2017-03-01 1 views
0

Ich versuche eine automatische Abmeldung bei Inaktivität mit JavaScript zu machen. Die Sache ist, selbst wenn ich meine Maus bewege und eine beliebige Taste schnell drücke, wird der Timer nicht zurückgesetzt.Mousemove und Tastendruck funktioniert nicht

var timeOut; 
function reset() { 
window.clearTimeout(timeOut); 
timeOut = window.setTimeout("redir()" , 5000); 
} 
function redir() { 
window.location = "index.php?page=Logout"; 
} 
window.onload = function() { setTimeout("redir()" , 5000) }; 
window.onmousemove = reset; 
window.onkeypress = reset; 

Antwort

0

Stellen Sie die timeOut Variable in Ihrer onload Veranstaltung:

window.onload = function() { timeOut = setTimeout("redir()" , 5000) }; 

Andernfalls clearTimeout nicht den ersten Zeitgeber löschen.

Beachten Sie auch, dass es am besten auf die Funktionen in setTimeout() direkt beziehen, wie folgt aus:

window.onload = function() { setTimeout(redir, 5000) }; 

Arbeits Snippet:

var timeOut; 
 

 
function reset() { 
 
    window.clearTimeout(timeOut); 
 
    timeOut = window.setTimeout(redir, 5000); 
 
} 
 

 
function redir() { 
 
    console.log('redirected'); 
 
} 
 

 
window.onload = function() { 
 
    timeOut = setTimeout(redir, 5000) 
 
} 
 

 
window.onmousemove = reset; 
 
window.onkeypress = reset;

+1

dies funktioniert, vielen Dank! –