php ajax自动注销与计时器

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

这是我的自动注销脚本,

我想显示倒数计时器,如何创建和显示计时器,

另外,当用户点击页面正文时,我想活着,

计时器应该重置,然后在系统空闲时重新启动,

怎么做,

(计时器应该显示,也就是说,当人们不接触系统时,计时器应该运行,

如果用户触摸系统,则计数器应重新启动)

使用此function:

 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分钟)。

  

我的代码在这里……经过一段时间的修改……它对我有用……

 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; }