簡體   English   中英

addEventListener 沒有動作?

[英]addEventListener to no action?

如果用戶在最后 3 秒內沒有在頁面上進行任何操作,我希望彈出一個模式。 有沒有辦法讓 addEventListener 在 3 秒內無操作時運行 function?

使用setTimeout()在 3 秒后打開模式,並使用事件偵聽器進行按鍵(或按鍵等)來重置超時。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM