简体   繁体   English

SetInterval在更改页面时保持设置

[英]SetInterval keeps setting when changing pages

So, I have a JavaScript issue that is probably as old as time, but I can't seem to figure it out. 因此,我遇到了一个JavaScript问题,它可能与时间一样古老,但是我似乎无法弄清楚。 I have a form that takes up the whole page and has next and back buttons so the user can travel back and forth through the questions. 我有一个表格,它占据了整个页面,并具有“下一步”和“后退”按钮,以便用户可以在问题之间来回移动。 ( Single page app , no full refresh. If there was this would be easy!) 单页应用程序 ,没有完全刷新。如果有的话,这很容易!)

One of the requirements is to have a timeout function. 要求之一是具有超时功能。 So after they are idle after a cretin amount of time it logs them out to protect their information. 因此,在空闲时间过长之后,它们会注销以保护其信息。 The issue is that my interval keeps adding a new one every time I flip back and forth new pages. 问题是每次我来回翻动新页面时,我的间隔都会不断增加一个新的间隔。 Thus creating multiple timers and insanity. 从而造成多个计时器和精神错乱。

My code kind of looks like this. 我的代码看起来像这样。

 var idle = 0;
 var idleIntervalTimer = setInterval(timerIncrement, 1000); //This is what keeps being called
 var counter = 0;
 var timeout = 30;
 var count = 20;
 var resetCount = 20;

document.onmousemove = function() {
  if(app.timer.get('available') === 'Yes'){ //I'm just using this to make sure that they have not already been logged out
       count = resetCount;
       idle = 0;
       app.warning.close();
    }
};

function timerIncrement() {
  if(app.timer.get('available') === 'Yes'){
     console.log(idle);
     idle = idle + 1;

     if (idle > 19 && count >= 0) { 
        count = count;
        app.warning.show({ //This is just a warning message that shows up at the top of the page counting down before the app closes.
           message: 'Logging out in <span class="warn">' + count + '</span> seconds.'
        });

      if(count != null || count >= 0){
          count=count-1;

         if (count === 0 && count != 'null'){
             resetTimer();
             app.workflow.resetWorkflow();
         }
       }
     }
   }
 }


function resetTimer(){
   count = resetCount;
   idle = 0;
   clearInterval(counter);
   clearInterval(idleIntervalTimer);
    app.warning.close();
}

I have tried a few different things. 我尝试了一些不同的事情。 One being that I would set a blank variable at the top and use that blank variable to set the interval, but that didn't seem to work. 一种情况是,我将在顶部设置一个空白变量,并使用该空白变量来设置时间间隔,但这似乎不起作用。 I have also tried to detect if the interval was still active, but I don't really want to clear the interval, just not create another. 我还尝试检测该间隔是否仍处于活动状态,但是我真的不想清除该间隔,只是不想创建另一个间隔。 Some help would be apprenticed very much! 一些帮助将非常徒劳!

I'm assuming that code gets called on every page flip. 我假设代码在每次翻页时都会被调用。 Don't set the interval timer immediately. 不要立即设置间隔计时器。 You should create a function that will first clear it and then set. 您应该创建一个函数,该函数将首先清除它然后设置。

For example do this instead: 例如,执行以下操作:

var idleIntervalTimer;

function initIdleIntervalTimer(){
 clearInterval(idleIntervalTimer);
 idleIntervalTimer = setInterval(timerIncrement, 1000);
}

initIdleIntervalTimer();

Or try switching it the timer to the global scope, get rid of var idleIntervalTimer and do this: 或者尝试将其计时器切换到全局范围,摆脱var idleIntervalTimer并执行以下操作:

clearInterval(window.idleIntervalTimer);
window.idleIntervalTimer = setInterval(timerIncrement, 1000);

It's a little ugly, but 有点难看,但是

 window.clearInterval(localStorage['idleInterval'])
 var idleIntervalTimer = setInterval(timerIncrement, 1000); //This is what keeps being called
 window.localStorage['idleInterval'] = idleIntervalTimer;

EDIT: If you dont want to clear, wrap the entire init in a flag, eg 编辑:如果您不想清除,将整个init包装在一个标志中,例如

if (!window._intervalInitOccured){
    window._intervalInitOccured = true;
    //continue init

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM