闲置10秒后如何显示弹出式窗口

时间:2019-04-02 07:31:32

标签: javascript jquery

我想在用户不活动10秒后显示价格警报弹出。现在,弹出窗口仅在单击时出现。我要替换它。

我尽了最大努力,但没能完成。

$(document).ready(function () {
  var idleInterval = setInterval(inActiveTimer, 1000);
  $(this).mousemove(function (e) {
    idleTime = 0;
  });
  $(this).keypress(function (e) {
    idleTime = 0;
  });
  $(".sleepy-close, .sleepy-overlay, .sleepy-wake-up").click(function () {
    $(".sleepy-overlay").hide();
    clearInterval(idleInterval);
  });
});

代码的最后一部分将导致启用点击的弹出窗口。在闲置10秒钟后,如何使用自动弹出窗口替换它。

5 个答案:

答案 0 :(得分:1)

这将帮助您捕获screen中10秒钟的不活动状态。根据需要更改代码。

document.body.innerText = "hello count the seconds";
setIdleTimeout(10000, function() {
    document.body.innerText = "Where did you go?";
}, function() {
    document.body.innerText = "Welcome back!";
});



function setIdleTimeout(millis, onIdle, onUnidle) {
    var timeout = 0;
    startTimer();

    function startTimer() {
        timeout = setTimeout(onExpires, millis);
        document.addEventListener("mousemove", onActivity);
        document.addEventListener("keypress", onActivity);
    }
    
    function onExpires() {
        timeout = 0;
        onIdle();
    }

    function onActivity() {
        if (timeout) clearTimeout(timeout);
        else onUnidle();
        //since the mouse is moving, we turn off our event hooks for 1 second
        document.removeEventListener("mousemove", onActivity);
        document.removeEventListener("keypress", onActivity);
        setTimeout(startTimer, 1000);
    }
}

答案 1 :(得分:0)

要重置setInterval,您需要清除并重新设置。使用以下代码:

// Instead of idleTime = 0
clearInterval(idleInterval);
idleInterval = setInterval(inActiveTimer, 10000);

有关更多信息,请参见this answer

答案 2 :(得分:0)

尝试一下。

 var idleTime = 0;

 $(document).ready(function () {

  var idleInterval = setInterval(function(){ 
      if(idleTime >= 10){
        $(".sleepy-overlay").hide(); // enabling the popup 
        idleTime = 0;
      }else{ 
        idleTime++;
      } 
    }, 1000); 

  $(this).mousemove(function (e) {
   idleTime = 0;
  });

  $(this).keypress(function (e) {
   idleTime = 0;
  });

});

答案 3 :(得分:0)

尝试一下

var idleTime = 0;
$(document).ready(function () {
  //Increment the idle time counter every 10 seconds.
  var idleInterval = setInterval(timerIncrement, 10000); 

  //Zero the idle timer on mouse movement.
  $(this).mousemove(function (e) {
    idleTime = 0;
  });
  $(this).keypress(function (e) {
    idleTime = 0;
  });
  //Zero the idle timer on touch events.
  $(this).bind('touchstart', function(){
   idleTime = 0;
  });
  $(this).bind('touchmove', function(){
   idleTime = 0;
  });
});

function timerIncrement() {
  idleTime = idleTime + 1;
  if (idleTime > 1) { 
    alert("ok");
  }
}

答案 4 :(得分:0)

纯JavaScript方法

您可以使用setTimeout将计时器保持10秒钟,并在活动时清除计时器并立即重新启动。

var timeout;

function resetTimer(){
  clearTimeout(timeout);
  console.log("Clearing timer because of activity");
  timeout = setTimeout(function(){
    alert("Done with 10 Seconds!");
    //Trigger your popup here
  }, 10000);
}

document.onmousemove = resetTimer;
document.onkeypress = resetTimer;
//You can add more activity event listeners like click etc.