Last active
March 24, 2023 02:49
-
-
Save Pierowheelz/36f7932ed00c4936208050e3d435e37b to your computer and use it in GitHub Desktop.
Display a popup (with id="popup_expiredpage") when a page has expired
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const wb_expiry = new function(){ | |
self.load_time = 99999999999999; //set automatically | |
self.page_expiry = 60; //minutes | |
self.reload_timer = {}; | |
this.hidden = 'hidden'; | |
this.vis_change = 'visibilitychange'; | |
this.hidden_state = 'visibilityState'; | |
this.currently_visible = true; | |
this.init = () => { | |
self.load_time = new Date().getTime(); | |
this.set_page_visibility_prefixes(); | |
document.addEventListener(this.vis_change, this.visibility_change); | |
self.reload_timer = setInterval( self.check_time, 60000 ); // once per minute | |
}; | |
this.set_page_visibility_prefixes = () => { | |
if (typeof document.mozHidden !== "undefined") { | |
this.hidden = "mozHidden"; | |
this.vis_change = "mozvisibilitychange"; | |
this.hidden_state = "mozVisibilityState"; | |
} else if (typeof document.msHidden !== "undefined") { | |
this.hidden = "msHidden"; | |
this.vis_change = "msvisibilitychange"; | |
this.hidden_state = "msVisibilityState"; | |
} else if (typeof document.webkitHidden !== "undefined") { | |
this.hidden = "webkitHidden"; | |
this.vis_change = "webkitvisibilitychange"; | |
this.hidden_state = "webkitVisibilityState"; | |
} | |
}; | |
this.visibility_change = (e) => { | |
if( this.hidden == document[ this.hidden_state ] || 'hidden' == document[ this.hidden_state ] ){ | |
this.currently_visible = false; | |
} else { | |
this.currently_visible = true; | |
self.check_time(); | |
} | |
}; | |
self.check_time = function(){ | |
const cur_time = new Date().getTime(); | |
console.log('Checking page time: ', cur_time - self.load_time, '<', self.page_expiry * 60 * 1000 ); | |
if( cur_time - self.load_time > self.page_expiry * 60 * 1000 ){ | |
console.log(' - Page expired. Prompting user to reload'); | |
clearInterval( self.reload_timer ); | |
// Show popup over the screen | |
document.documentElement.classList.add('noscroll'); | |
const reloadPopup = document.querySelector('#popup_expiredpage'); | |
reloadPopup.style.display = "block"; | |
window.location.reload(); | |
} else { | |
console.log(' - Ok.'); | |
} | |
}; | |
// see if DOM is already available | |
if (document.readyState === "complete" || document.readyState === "interactive") { | |
// call on next available tick | |
setTimeout(this.init, 1); | |
} else { | |
document.addEventListener("DOMContentLoaded", this.init); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment