如何弹出消息并将过期时间设置为存储在本地存储中的消息状态?


if(localStorage.getItem('popState') != 'shown'){
$(function () {
$('[data-toggle="popover"]').popover({
content : "....."
});
$('[data-toggle="popover"]').popover('show');
});
localStorage.setItem('popState','shown')
}

我正在使用上述方法在页面加载期间向用户显示弹出消息,并第二次禁用加载后要显示的弹出消息。如何让它在一段时间后自动弹出给用户?例如,在用户关闭弹出消息后,它将在一小时后自动显示。

您可以为此使用间隔:

const showPopup = function showPopup() {
const lastShown = localStorage.getItem('popStateLastShown');
const hasOneHourPassed = lastShown ?
(Math.abs(new Date(lastShown) - new Date()) / 36e5) >= 1 :
false;
if (hasOneHourPassed || localStorage.getItem('popState') !== 'shown') {
// Show popup
localStorage.setItem('popState', 'shown');
localStorage.setItem('popStateLastShown', new Date());
}
};
// Run code immediately.
showPopup();
// Check again after an hour.
setInterval(showPopup, 36e5);

最新更新