我正在博物馆展示一个与滚动相关的网站项目,需要在用户之间进行重置。我希望设置一个计时器,当用户离开电脑后,将页面滚动到顶部并重新加载。我已经进行了一些研究,目前这是我确定下来的方案。如果用户在两分钟内没有滚动,则会重置页面:
// Setup isScrolling variable
let isScrolling;
// Listen for scroll events
window.addEventListener('scroll', event => {
// Clear our timeout throughout the scroll
window.clearTimeout(isScrolling);
// Set a timeout to run after scrolling ends
isScrolling = setTimeout(() => {
// scroll to top and refresh page
console.log('Scrolling has stopped.');
window.scrollTo(0,0);
location.reload();
}, 120000);
}, false);
这是解决这个问题的最佳方法吗?每次滚动都运行事件监听器会很耗费资源,还是说这基本上是正确的做法?
location.reload()
之前调用scrollTo(0,0)
?页面刷新已经重置了页面,对吧? - Rob Monhemius