(function() {
// 创建弹窗的 HTML 结构
const popupHTML = `
`;
// 将弹窗 HTML 添加到页面中
document.body.insertAdjacentHTML('beforeend', popupHTML);
// 获取弹窗容器和相关元素
const popupContainer = document.getElementById('popup-container');
const countdownTimer = document.getElementById('countdown-timer');
const closeButton = document.getElementById('close-button');
// 设置倒计时的初始时间(单位:秒)
let remainingTime = 3;
// 获取当前日期(格式:YYYY-MM-DD)
const today = new Date().toISOString().split('T')[0];
// 检查 localStorage 中是否有记录的日期
const lastShownDate = localStorage.getItem('popupLastShownDate');
// 如果今天没有显示过弹窗,则显示弹窗
if (lastShownDate !== today) {
// 延迟 2 秒后显示弹窗
setTimeout(() => {
popupContainer.style.display = 'flex'; // 显示弹窗
// 启动倒计时
const countdownInterval = setInterval(() => {
remainingTime--; // 每秒减少 1
countdownTimer.textContent = remainingTime; // 更新倒计时显示
// 如果时间到达 0,关闭弹窗并停止倒计时
if (remainingTime <= 0) {
clearInterval(countdownInterval); // 停止倒计时
popupContainer.style.display = 'none'; // 隐藏弹窗
}
}, 1000); // 每 1000 毫秒(1 秒)触发一次
// 点击按钮立即关闭弹窗
closeButton.addEventListener('click', function() {
clearInterval(countdownInterval); // 停止倒计时
popupContainer.style.display = 'none'; // 隐藏弹窗
});
// 记录弹窗显示日期到 localStorage
localStorage.setItem('popupLastShownDate', today);
}, 2000); // 延迟 2 秒后显示弹窗
}
})();