Play history
2012
United States
Get the element
const gonggao = document.getElementById('gonggao');
const closeBtn = gonggao.querySelector('.gonggao-close');
const confirmBtn = gonggao.querySelector('.gonggao-confirm');
Store the key name
const STORAGE_KEY = 'gonggao_closed_time';
Check if the pop-up window should appear
function shouldShowGonggao() {
const closedTime = localStorage.getItem(STORAGE_KEY);
if (!closedTime) {
return true; Never closed, shown
}
const now = new Date().getTime();
const diff = now - parseInt(closedTime);
If it exceeds 24 hours, a pop-up window will appear
return diff > 24 * 60 * 60 * 1000;
}
A pop-up window is displayed
function showGonggao() {
gonggao.classList.add('active');
document.body.style.overflow = 'hidden'; Prevent background scrolling
}
Close the pop-up window and log the time
function closeGonggao() {
gonggao.classList.remove('active');
document.body.style.overflow = 'auto'; Resume background scrolling
Record the shutdown time
localStorage.setItem(STORAGE_KEY, new Date().getTime().toString());
}
Automatically display pop-ups after page loading (if needed)
document.addEventListener('DOMContentLoaded', function() {
Delay the display by 1 second to let the page load first
setTimeout(function() {
if (shouldShowGonggao()) {
showGonggao();
}
}, 1000);
});
bind closure events
closeBtn.addEventListener('click', closeGonggao);
confirmBtn.addEventListener('click', closeGonggao);
Click Background Off
gonggao.addEventListener('click', function(e) {
if (e.target === gonggao) {
closeGonggao();
}
});
ESC key is off
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape' && gonggao.classList.contains('active')) {
closeGonggao();
}
});