Автоматический выход из системы php ajax с таймером

<script type="text/javascript"> var t; function startTimer(){ t=setTimeout("document.location='../login/logout.php'", 50000); } function stopTimer(){ clearTimeout(t); } </script> 

Это мой скрипт для автоматического выхода из системы,

я хочу показать таймер обратного отсчета, как создать и показать таймер,

Также я хочу сделать живым, когда пользователь ударит по телу страницы,

Также таймер должен перезагрузиться, а затем снова перезапустить, когда система не работает,

Как это сделать,

(Таймер должен показывать, то есть таймер должен запускаться, когда люди не касаются системы,

если пользователь прикасается к системе, то счетчик должен перезапустить)

Используйте эту функцию:

 function timer(elem, starttime, endtime, speed, funktion, count) { if (!endtime) endtime = 0; if (!starttime) starttime = 10; if (!speed) speed = 1; speed = speed * 1000; if ($(elem).html() || $(elem).val()) { if (count == "next" && starttime > endtime) starttime--; else if (count == "next" && starttime < endtime) starttime++; if ($(elem).html()) $(elem).html(starttime); else if ($(elem).val()) $(elem).val(starttime); if (starttime != endtime && $(elem).html()) setTimeout(function() { timer(elem, $(elem).html(), endtime, speed / 1000, funktion, 'next'); }, speed); if (starttime != endtime && $(elem).val()) setTimeout(function() { timer(elem, $(elem).val(), endtime, speed / 1000, funktion, 'next'); }, speed); if (starttime == endtime && funktion) funktion(); } else return; } 

пример

 timer("#timer", 50, 0, 1, function() { location.href = "../login/logout.php"; }); 

мой пример:

Обновлено, чтобы проверить, свободен ли пользователь (установлен на 2 секунды, это упрощает тестирование, я бы рекомендовал по крайней мере 5 или 10 минут).

 <body onload="setTimeout('startCountDown()',2000);" onmousemove="resetTimer();"> <form name="counter"><input type="text" size="5" name="timer" disabled="disabled" /></form> <script type="text/javascript"> <!-- // edit startSeconds as you see fit // simple timer example provided by Thomas var startSeconds = 10; var milisec = 0; var seconds=startSeconds; var countdownrunning = false var idle = false; document.counter.timer.value=startSeconds; function CountDown() { if(idle == true) { if (milisec<=0) { milisec=9 seconds-=1 } if (seconds<=-1) { document.location='../login/logout.php'; milisec=0 seconds+=1 return; } else milisec-=1; document.counter.timer.value=seconds+"."+milisec; setTimeout("CountDown()",100); } else { return; } } function startCountDown() { document.counter.timer.value=startSeconds; seconds = startSeconds; milisec = 0 document.counter.timer.style.display = 'block'; idle = true; CountDown(); document.getElementById("alert").innerHTML = 'You are idle. you will be logged out after ' + startSeconds + ' seconds.'; countdownrunning = false; } function resetTimer() { document.counter.timer.style.display = 'none'; idle = false; document.getElementById("alert").innerHTML = ''; if(!countdownrunning) setTimeout('startCountDown()',2000); countdownrunning = true; } --> </script> , <body onload="setTimeout('startCountDown()',2000);" onmousemove="resetTimer();"> <form name="counter"><input type="text" size="5" name="timer" disabled="disabled" /></form> <script type="text/javascript"> <!-- // edit startSeconds as you see fit // simple timer example provided by Thomas var startSeconds = 10; var milisec = 0; var seconds=startSeconds; var countdownrunning = false var idle = false; document.counter.timer.value=startSeconds; function CountDown() { if(idle == true) { if (milisec<=0) { milisec=9 seconds-=1 } if (seconds<=-1) { document.location='../login/logout.php'; milisec=0 seconds+=1 return; } else milisec-=1; document.counter.timer.value=seconds+"."+milisec; setTimeout("CountDown()",100); } else { return; } } function startCountDown() { document.counter.timer.value=startSeconds; seconds = startSeconds; milisec = 0 document.counter.timer.style.display = 'block'; idle = true; CountDown(); document.getElementById("alert").innerHTML = 'You are idle. you will be logged out after ' + startSeconds + ' seconds.'; countdownrunning = false; } function resetTimer() { document.counter.timer.style.display = 'none'; idle = false; document.getElementById("alert").innerHTML = ''; if(!countdownrunning) setTimeout('startCountDown()',2000); countdownrunning = true; } --> </script> , <body onload="setTimeout('startCountDown()',2000);" onmousemove="resetTimer();"> <form name="counter"><input type="text" size="5" name="timer" disabled="disabled" /></form> <script type="text/javascript"> <!-- // edit startSeconds as you see fit // simple timer example provided by Thomas var startSeconds = 10; var milisec = 0; var seconds=startSeconds; var countdownrunning = false var idle = false; document.counter.timer.value=startSeconds; function CountDown() { if(idle == true) { if (milisec<=0) { milisec=9 seconds-=1 } if (seconds<=-1) { document.location='../login/logout.php'; milisec=0 seconds+=1 return; } else milisec-=1; document.counter.timer.value=seconds+"."+milisec; setTimeout("CountDown()",100); } else { return; } } function startCountDown() { document.counter.timer.value=startSeconds; seconds = startSeconds; milisec = 0 document.counter.timer.style.display = 'block'; idle = true; CountDown(); document.getElementById("alert").innerHTML = 'You are idle. you will be logged out after ' + startSeconds + ' seconds.'; countdownrunning = false; } function resetTimer() { document.counter.timer.style.display = 'none'; idle = false; document.getElementById("alert").innerHTML = ''; if(!countdownrunning) setTimeout('startCountDown()',2000); countdownrunning = true; } --> </script> , <body onload="setTimeout('startCountDown()',2000);" onmousemove="resetTimer();"> <form name="counter"><input type="text" size="5" name="timer" disabled="disabled" /></form> <script type="text/javascript"> <!-- // edit startSeconds as you see fit // simple timer example provided by Thomas var startSeconds = 10; var milisec = 0; var seconds=startSeconds; var countdownrunning = false var idle = false; document.counter.timer.value=startSeconds; function CountDown() { if(idle == true) { if (milisec<=0) { milisec=9 seconds-=1 } if (seconds<=-1) { document.location='../login/logout.php'; milisec=0 seconds+=1 return; } else milisec-=1; document.counter.timer.value=seconds+"."+milisec; setTimeout("CountDown()",100); } else { return; } } function startCountDown() { document.counter.timer.value=startSeconds; seconds = startSeconds; milisec = 0 document.counter.timer.style.display = 'block'; idle = true; CountDown(); document.getElementById("alert").innerHTML = 'You are idle. you will be logged out after ' + startSeconds + ' seconds.'; countdownrunning = false; } function resetTimer() { document.counter.timer.style.display = 'none'; idle = false; document.getElementById("alert").innerHTML = ''; if(!countdownrunning) setTimeout('startCountDown()',2000); countdownrunning = true; } --> </script> 

мой код здесь … после небольшого изменения … он работает для меня …

 var startSeconds = 10; var milisec = 0; var seconds=startSeconds; var countdownrunning = false var idle = false; document.counter.timer.value=startSeconds; function CountDown() { if(idle == true) { if (milisec<=0) { milisec=9 seconds-=1 } if (seconds<=-1) { document.location='../login/logout.php'; milisec=0 seconds+=1 return; } else seconds-=1; setTimeout("CountDown()",1000); } else { return; } } function startCountDown() { seconds = startSeconds; milisec = 0 idle = true; CountDown(); document.getElementById("alert").innerHTML = 'You are idle. you will be logged out after ' + startSeconds + ' seconds.'; countdownrunning = false; } function resetTimer() { idle = false; if(countdownrunning) setTimeout('startCountDown()',2000); countdownrunning = true; } , var startSeconds = 10; var milisec = 0; var seconds=startSeconds; var countdownrunning = false var idle = false; document.counter.timer.value=startSeconds; function CountDown() { if(idle == true) { if (milisec<=0) { milisec=9 seconds-=1 } if (seconds<=-1) { document.location='../login/logout.php'; milisec=0 seconds+=1 return; } else seconds-=1; setTimeout("CountDown()",1000); } else { return; } } function startCountDown() { seconds = startSeconds; milisec = 0 idle = true; CountDown(); document.getElementById("alert").innerHTML = 'You are idle. you will be logged out after ' + startSeconds + ' seconds.'; countdownrunning = false; } function resetTimer() { idle = false; if(countdownrunning) setTimeout('startCountDown()',2000); countdownrunning = true; } , var startSeconds = 10; var milisec = 0; var seconds=startSeconds; var countdownrunning = false var idle = false; document.counter.timer.value=startSeconds; function CountDown() { if(idle == true) { if (milisec<=0) { milisec=9 seconds-=1 } if (seconds<=-1) { document.location='../login/logout.php'; milisec=0 seconds+=1 return; } else seconds-=1; setTimeout("CountDown()",1000); } else { return; } } function startCountDown() { seconds = startSeconds; milisec = 0 idle = true; CountDown(); document.getElementById("alert").innerHTML = 'You are idle. you will be logged out after ' + startSeconds + ' seconds.'; countdownrunning = false; } function resetTimer() { idle = false; if(countdownrunning) setTimeout('startCountDown()',2000); countdownrunning = true; }