javascript - 模态窗口应该每天只为每位访客打开一次
问题描述
我创建了一个模态窗口,几秒钟后向网站访问者显示以通知新的东西。
我使用的代码如下:
<div id="ex1" class="modal">
<h1 style="color:#395CC3">We need you !</h1><br>
<p>Lots of text...</p>
<div align="right"> <a href="mailto:contatti@andreadd.it" style="color:
#395CC3; text-decoration:none">CONTATTACI !!!</a></div>
</div>
<script type="text/javascript">
setTimeout(function(event) {
$('#ex1').modal({
fadeDuration: 500,
fadeDelay: 1.50,
escapeClose: false,
});
}, 10000);
</script>
该代码可以正常工作,但我想将此代码放在所有网站页面上。我需要如果弹出窗口显示给访问者,那么它至少在一天内不会再显示给该访问者。
你能帮我添加可以做到这一点的代码部分吗?非常感谢你!
解决方案
您可以使用 localStorage 来存储上次显示的时间。LocalStorage w3schools
let lastShown = parseInt(localStorage.getItem('lastShown')); //EDIT: Added parseInt
let maxTime = 1000; //ms (1 second)
//if lastShown is undefined or enough time has passed
if(!lastShown | lastShown + maxTime < Date.now()) {
//show it
//store the time to check next time the page is loaded
localStorage.setItem('lastShown', Date.now());
}
推荐阅读
- c# - 试图在 C# 中结合 Luis 和附件
- docker - Split health checks cannot set custom path
- c++ - 将整数范围映射到另一个范围
- python - 如何按相同字段合并字典列表并在此过程中对另一个字段求和?
- c - 为什么我永远不会超过 1?
- r - 如何删除 R 中 heatmap.2 中的列名?
- arrays - leetcode 287 的时间复杂度
- javascript - 如何更改默认音频输入
- amazon-web-services - AWS Lambda 打印运行 lambda 函数的“当前用户 ID”(不是账户 ID)
- continuous-integration - 如何将退出代码从自定义测试框架传递到 CircleCI 以便在必要时使该步骤失败