Skip to content

Instantly share code, notes, and snippets.

@thybzi thybzi/idle-refresh.js
Last active Apr 10, 2017

Embed
What would you like to do?
(function() {
window.lastActivityTime = 0;
window.refreshInterval = 10000;
window.idleInterval = 60000;
resetRefreshTimer();
document.body.addEventListener('mousemove', resetRefreshTimer);
document.body.addEventListener('keypress', resetRefreshTimer);
setTimeout(refresh, window.refreshInterval);
if (document.addEventListener) {
if ('onwheel' in document) {
// IE 9+, Firefox 17+, Chrome 31+
document.addEventListener('wheel', onWheel);
} else if ('onmousewheel' in document) {
// old event name
document.addEventListener('mousewheel', onWheel);
} else {
// Firefox 16-
document.addEventListener('MozMousePixelScroll', onWheel);
}
} else {
// IE 8-
document.attachEvent('onmousewheel', onWheel);
}
function onWheel(e) {
var delta, scrollBy;
e = e || window.event;
if (e.altKey) {
// wheelDelta doesn't contain the amount of scrolled pixels
delta = e.deltaY || e.detail || e.wheelDelta;
scrollBy = (delta > 0) ? 100 : -100;
window.scrollBy(scrollBy, 0);
e.preventDefault ? e.preventDefault() : (e.returnValue = false);
}
}
})();
function resetRefreshTimer() {
window.lastActivityTime = new Date().getTime();
}
function refresh() {
if ((new Date().getTime() - window.lastActivityTime) >= window.idleInterval) {
window.location.reload(true);
} else {
setTimeout(refresh, window.refreshInterval);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.