2016-11-09 39 views
0

我想重置計時器在用戶打字輸入或點擊屏幕。 這是我的代碼,但它不會重置計時器。 你能告訴我問題在哪裏,應該如何解決?復位定時器,而用戶鍵入或點擊屏幕

$(document).ready(function() { 
    $(".data-lock").click(function() { 
     $(this).animate({ 
      bottom: '100%' 
     }, function() { 
      $(".back-img").addClass("lock-up"); 
      $(".logbox").addClass("logboxinfead"); 
      setTimeout(function() { 
       $(".data-lock").animate({ 
        bottom: '0' 
       }, function() { 
        $(".back-img").removeClass("lock-up"); 
        $(".logbox").removeClass("logboxinfead"); 
       }); 
      }, 30000); 
     }); 
    }); 
    var idleInterval = setInterval(timerIncrement(), 60000); // 1 minute 

    //Zero the idle timer on mouse movement. 
    $(this).mousemove(function(e) { 
     idleTime = 0; 
    }); 
    $(this).keypress(function(e) { 
     idleTime = 0; 
    }); 
}); 

function timerIncrement() { 
    idleTime = idleTime + 1; 
    if (idleTime > 19) { // 20 minutes 
     window.location.reload(); 
    } 
}); 
+0

你可以把相關的HTML代碼,以方便「複製和粘貼」再現並盡力幫助你嗎? – OscarAkaElvis

回答

0

你要叫「clearInterval」停止定時器如下。我還在下面將'mousemove'改爲'click'。

$(this).click(function (e) { 
     idleTime = 0; 
     if(idleInterval) { 
      clearInterval(idleInterval); 
      idleInterval = null; 
     } 
    }); 
    $(this).keypress(function (e) { 
     idleTime = 0; 
     if(idleInterval) { 
      clearInterval(idleInterval); 
      idleInterval = null; 
     } 
    }); 
相關問題