我希望弹出一个模式,如果在过去的3秒内没有由用户在页面上的行动。有没有办法让addEventListener在3秒内没有动作的情况下运行函数?
发布于 2020-07-21 02:42:16
使用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();
};https://stackoverflow.com/questions/63001479
复制相似问题