Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 多页倒计时_Javascript_Php_Timer_Dom Events - Fatal编程技术网

Javascript 多页倒计时

Javascript 多页倒计时,javascript,php,timer,dom-events,Javascript,Php,Timer,Dom Events,我在主页上有一个计时器。当用户进行任何操作时,如移动鼠标,计时器将重置。当计时器倒计时到05:00时,系统将弹出警报,通过单击“确定”重置计时器。如果在5分钟内没有任何操作,并且计时器倒计时到00:00,则用户将被迫注销。 我的问题是这个计时器只能在一页内工作,如果我打开了两页,计时器只能在焦点页上重置。由于页面未聚焦,我仍将注销 有人能帮我吗?非常感谢 <script type="text/javascript"> var flag = false; startTimer(

我在主页上有一个计时器。当用户进行任何操作时,如移动鼠标,计时器将重置。当计时器倒计时到05:00时,系统将弹出警报,通过单击“确定”重置计时器。如果在5分钟内没有任何操作,并且计时器倒计时到00:00,则用户将被迫注销。 我的问题是这个计时器只能在一页内工作,如果我打开了两页,计时器只能在焦点页上重置。由于页面未聚焦,我仍将注销

有人能帮我吗?非常感谢

<script type="text/javascript">
  var flag = false;
  startTimer();
  function startTimer() {
    var presentTime = $("#Status").text().toString();
    var timeArray = presentTime.split(":");
    var m = timeArray[0];
    var s = checkSecond((timeArray[1] - 1));
    if(s==59) {
      m=m-1;
      if(m < 10) {
        m = "0" + m;
      }
    }

    if (m==05 && s==00) {
      flag = true;
      BootstrapDialog.alert("You have 5 minutes remaining in your session. Click \"OK\" to extend the session.", function() {
        flag = false;
        resetTimer();
      });
    }

    if(!flag) {
      //Reset timer upon user action
      document.onload = resetTimer;
      document.onmousemove = resetTimer;
      document.onmousedown = resetTimer;
      document.ontouchstart = resetTimer;
      document.onclick = resetTimer;
      document.onscroll = resetTimer;
      document.onkeydown = resetTimer;
    }
    else {
      document.onload = null;
      document.onmousemove = null;
      document.onmousedown = null;
      document.ontouchstart = null;
      document.onclick = null;
      document.onscroll = null;
      document.onkeydown = null;
    }

    if (m==0 && s==00) {
      window.location.replace("/Logout");
    };

    setTimeout(startTimer, 1000);
  }

  function checkSecond(sec) {
    if (sec < 10 && sec >= 0) {sec = "0" + sec};
    if (sec < 0) {sec = "59"};
    return sec;
  }

  function resetTimer() {
    $("#Status").html("30:00");
  }
</script>

<div> Your session has <h5 id="Status">30:00</h5> remaining. </div>

var标志=假;
startTimer();
函数startTimer(){
var presentTime=$(“#Status”).text().toString();
var timeArray=presentTime.split(“:”);
var m=时间数组[0];
var s=校验秒((时间数组[1]-1));
如果(s==59){
m=m-1;
如果(m<10){
m=“0”+m;
}
}
如果(m==05&&s==00){
flag=true;
BootstrapDialog.alert(“您的会话还有5分钟。单击“确定”以扩展会话。”,函数(){
flag=false;
重置计时器();
});
}
如果(!标志){
//用户操作时重置计时器
document.onload=重置计时器;
document.onmousemove=resetTimer;
document.onmousedown=重置计时器;
document.ontouchstart=重置计时器;
document.onclick=重置计时器;
document.onscroll=重置计时器;
document.onkeydown=重置计时器;
}
否则{
document.onload=null;
document.onmousemove=null;
document.onmousedown=null;
document.ontouchstart=null;
document.onclick=null;
document.onscroll=null;
document.onkeydown=null;
}
如果(m==0&&s==00){
window.location.replace(“/Logout”);
};
setTimeout(startTimer,1000);
}
功能检查秒(秒){
如果(秒<10&&sec>=0){sec=“0”+sec};
如果(sec<0){sec=“59”};
返回秒;
}
函数resetTimer(){
$(“#Status”).html(“30:00”);
}
您的会话还有30:00。

您的问题是您想用一个脚本控制两个页面,但每个页面中的变量是独立的

如果你解决了这个问题,你可以控制你的页面

您可以从cookie或localstorage获取变量m和s。如果cookie和localstorage中的变量包位于同一根目录中,则可以在不同的页面中访问它们


尝试一下,只需替换变量即可。从cookie或localstorage获取并设置它

您可以选择:在PHP中启动会话并在其中存储starttime。由于在所有选项卡中使用相同的PHP会话,因此您可以轻松检索两个选项卡中的值。或者使用为整个域设置的cookie并将开始时间存储在其中。然后从两个浏览器中轮询会话/cookie。对整个域使用cookie会很好,这是一个不错的想法。然后,同一域的所有选项卡都将访问同一个cookie以读取开始时间。还有一个新的名称,据说是为了解决此类问题而创建的。“但我从未使用过它。”米歇尔广播频道不支持IE.T_T@Michel我曾考虑使用$\u SESSION['remaining\u time']来存储当前剩余的内容,但我认为javascript无法将值传递给PHP。