This code immediately sets the timer for 15 seconds and redirects to the main page, how to make the timer appear if the user has been in AFC for some time?

<b><span id="time"></span></b> <script type="text/javascript"> var i = 15;//врСмя Π² сСк. function time(){ document.getElementById("time").innerHTML = i;//Π²ΠΈΠ·ΡƒΠ°Π»ΡŒΠ½Ρ‹ΠΉ счСтчик i--;//ΡƒΠΌΠ΅Π½ΡŒΡˆΠ΅Π½ΠΈΠ΅ счСтчика if (i < 0) location.href = "localhost/index.php";//Ρ€Π΅Π΄ΠΈΡ€Π΅ΠΊΡ‚ } time(); setInterval(time, 1000); </script> 
  • Track mouse position changes, for example. If the position does not change for a long time - redirect - ArchDemon
  • it always sounds great in words - LLIAKAJI

2 answers 2

Try this

 var i = 15, timer;//врСмя Π² сСк. function time(){ document.getElementById("time").innerHTML = i;//Π²ΠΈΠ·ΡƒΠ°Π»ΡŒΠ½Ρ‹ΠΉ счСтчик i--;//ΡƒΠΌΠ΅Π½ΡŒΡˆΠ΅Π½ΠΈΠ΅ счСтчика if (i < 0) { alert('ВрСмя ΠΊΠΎΠ½Ρ‡ΠΈΠ»ΠΎΡΡŒ'); location.href = "/index.php";//Ρ€Π΅Π΄ΠΈΡ€Π΅ΠΊΡ‚ clearInterval(timer); } } function reset() { i = 15; time(); } time(); timer = setInterval(time, 1000); document.onmousemove = reset; document.onkeypress = reset; 

    $ (window) .on ("blur", function () {}); - in case the user has moved to another tab, or window