首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >addEventListener to no action?

addEventListener to no action?
EN

Stack Overflow用户
提问于 2020-07-21 02:12:19
回答 1查看 90关注 0票数 2

我希望弹出一个模式,如果在过去的3秒内没有由用户在页面上的行动。有没有办法让addEventListener在3秒内没有动作的情况下运行函数?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-21 02:42:16

使用setTimeout()在3秒后打开模式,并使用按键(或向上键等)的事件侦听器来重置超时。

代码语言:javascript
复制
let timeout = undefined;

function resetTimer() {
  if (timeout) {
    clearTimeout(timeout);
  }

  timeout = setTimeout(function() {
    // open modal, call resetTimer() when it is closed to restart the check...
    /*
       modal.onClose = resetTimer;
       modal.open();
    */
    console.log('open modal');
  }, 3000);
}

window.onload = function() {
  // every time a key is pressed reset the timer
  document.addEventListener('keypress', resetTimer);
  // start the timer
  resetTimer();
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63001479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档