对于用户空闲超时,仅执行一次功能



我想在用户闲置超时5秒时显示示例横幅秒。任何身体都帮助我

$(document).ready(function(){
           idleTime = 0;        

           //Increment the idle time counter every five second.
           var idleInterval = setInterval(timerIncrement, 5000);
           function timerIncrement()
           {
             console.log(idleTime++);
             if (idleTime > 5)
             {
               doPreload();     
             }
           }
           //Zero the idle timer on mouse movement.
           $(this).mousemove(function(e){
                 idleTime = 0;
           });
           function doPreload()
           {
             $('#add-click').click();
           }   
        });

您仅使用鼠标移动,如果用户使用键盘工作,您还必须检查

var inactivityTime = function () {
var temp;
window.onload = resetTimer;
document.onmousemove = resetTimer;
document.onkeypress = resetTimer;
function resetTimer() {
    clearTimeout(temp);
    temp = setTimeout(logout, 3000)
    // 1000 milisec = 1 sec
 }
};

您需要使用setTimeout而不是setInterval

    window.setTimeout(function(){
        timerIncrement();
}, 5000);

这只会执行一次。

为什么不尝试settimeout而不是setInterval?

在此功能中纠正逻辑为:

var idleInterval = setInterval(timerIncrement, 1000);
  function timerIncrement() {
    console.log(idleTime++);
    if (idleTime == 5) {
        doPreload();
        idleTime = 0;   // reset idleTime variable     
      }
  }
   /** This will increase counter by one every second,
    *  and if counter reaches to 5 then call to banner
    *  display and reset counter. 
    */

最新更新