仅显示警报一次 [英] Display alert only once

查看:86
本文介绍了仅显示警报一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,它在日历中的事件发生前30分钟显示警告,但我想在用户访问页面时显示一次(在30分钟内)。现在它显示在每个页面刷新和日历刷新(在30分钟内),因为它被设置为在一段时间后刷新事件。如何只显示一次此警报?

I have this code that shows alert when it's 30 minutes before event in calendar but I want to show it only once when user comes to page (in that 30min). Now it shows on every page refresh and also on calendar refresh (in that 30min) because it is set to refresh events after period of time. How to display this alert just once?

var mili = event.start.getTime() - now.getTime();
if(mili < 1800000 && mili > 0){    
$('.alert').dialog({ 
        buttons: [{ 
        text: "OK", 
          click: function() { 
            $( this ).dialog( "close" ); 
          }
        }]
    });
}


推荐答案

您可以使用localStorage(不是与旧浏览器兼容):

You can use localStorage (not compatible with older browsers):

<script type="text/javascript">
    var alerted = localStorage.getItem('alerted') || '';
    if (alerted != 'yes') {
     alert("My alert.");
     localStorage.setItem('alerted','yes');
    }
</script>

或者你可以使用cookies,看看这个答案的完整示例代码:
https://stackoverflow.com/a/21567127/3625883

Or you can use cookies, give a look to this answer for a full example code: https://stackoverflow.com/a/21567127/3625883

这篇关于仅显示警报一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆