javascript - 如何在没有 cookie 的会话中仅显示一次警报信息框
问题描述
我在引导程序 3 中创建了一个警报信息框,并设法创建它在 10 秒后消失。但我希望这个框只显示一次浏览器会话(而不是在每个页面重新加载时显示现在的样子)。
不喜欢使用 cookie,而是使用 sessionStorage
这是我的html代码:
<div class="alert alert-info alert-success credits center" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<h2>text </h2> text 2
<div class="row margin-t-10">
<div class="col-md-3 col-xs-5 col1 center-block">
<a href="#"><button class="button btn-success btn-block" > text 3<span style="font-size:17px;" aria-hidden="true"></span></button></a>
</div>
</div>
</div>
这是我的 JS 代码:
window.setTimeout(function() {
$(".alert-credit, .alert-info").fadeTo(1000, 0).slideUp(1000, function(){
$(this).remove();
});
}, 9000);
我怎样才能知道警报信息框只显示一次会话?
解决方案
您只需要在sessionStorage
显示警报时保存一个条目,并在显示警报之前检查它是否存在:
if(sessionStorage.getItem('alertShown') === null){
window.setTimeout(function() {
$(".alert-credit, .alert-info").fadeTo(1000, 0).slideUp(1000, function(){
sessionStorage.setItem('alertShown', 'true');
$(this).remove();
});
}, 9000);
}else{
$(".alert-credit, .alert-info").remove();
}
推荐阅读
- c# - MSSQL DB - 在 db 中建立关系有什么好处?
- node.js - 如何在 Node JS 中将属性添加到对象中
- excel - 这个 Google Sheet 可以翻译成 Excel 吗?查找的多个结果
- python - 模糊-c-means - 设置初始簇数=6,但只生成 4 个簇标签
- c# - 我无法正确计算这个简单的计算
- python - 如何让 Ajax 删除 Django 的对象实例?
- go - 无法使用默认服务帐户和谷歌云库从谷歌 Kubernetes 引擎访问谷歌云存储
- java - Drive REST API 仅使用 wifi
- python - statsmodels 多重回归的附加步骤?
- mysql - JPA 非实体选择数据库结果到列表