1

在 mousemove 或 scroll 我想重置一个计时器,如果它没有运行并且在计时器函数内部运行一个函数一次。到目前为止,我有这个...

var timerId,
    lastActive = new Date().getTime(),
    token;

var timerFunc = function () {

   var currentTime = new Date().getTime();
   var timeDiff = currentTime - lastActive;
   if (timeDiff > 10000) {
     //clearInterval(timerId);
   }

      //I want to do some logic here
      // but only on the first iteration of the timer
      //how can I do that?

};

$(window).on('mousemove scroll', function (e) {
lastActive = new Date().getTime();

 //only restart the timer if its not currently running. How can I do that??
if(resetTimer)
       timerId = setInterval(timerFunc , 10000);            
 });

 timerId = setInterval(timerFunc , 10000);

任何 javascript 大师可以帮助我填写这些内容吗?如果我太简短了,我很抱歉。我会在评论中跟进任何问题。谢谢大家的任何提示、链接、技巧等。干杯。=)

4

1 回答 1

0

我会为第一个间隔使用一个布尔变量(这很简单,只需在计时器之后添加它。你在 if(resetTimer) 中也有正确的想法,检查它是否正在运行只需设置计时器函数运行时和停止时的全局变量。

于 2012-07-03T14:35:49.740 回答