简体   繁体   English

如何在 10 秒不活动后显示弹出窗口

[英]How to display a pop up after 10 seconds of inactivity

I want to display a price alarm pop up after 10 seconds of inactivity of user.我想在用户不活动 10 秒后弹出价格警报。 Right now the pop up appears only on click.现在弹出窗口仅在单击时出现。 I want to replace it.我想更换它。

I tried my best but couldn't get it done.我尽了最大的努力,但无法完成。

$(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);
  });
});

The last part of the code leads to click enabled pop up.代码的最后一部分导致单击启用弹出。 How can I replace it with automatic pop up which occurs after 10 seconds of inactivity.如何用 10 秒不活动后自动弹出来替换它。

this will help you to capture 10 sec of inactivity in screen.change the code as such u need .这将帮助您在屏幕中捕获 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); } }

To reset a setInterval , you need to clear it and set it again.要重置setInterval ,您需要将其清除并重新设置。 Use the following code:使用以下代码:

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

See this answer for more info有关更多信息,请参阅此答案

Try this.尝试这个。

 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;
  });

});

Pure JavaScript approach纯 JavaScript 方法

You can keep a timer for 10 seconds using setTimeout and clear it whenever their is activity and restart it immediately.您可以使用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.

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

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