Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display alert only once

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" ); 
          }
        }]
    });
}
like image 308
Jakob Avatar asked Jul 15 '14 20:07

Jakob


3 Answers

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>

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

like image 114
Filo Avatar answered Nov 06 '22 04:11

Filo


Set a cookie when you show the alert. Then, if the cookie is set, you will know not to show the alert again. If it is not set, you know that you haven't shown the alert yet and should do so now.

You can read about setting cookies in JavaScript here.

like image 2
elixenide Avatar answered Nov 06 '22 04:11

elixenide


I know this is late but you definitely don't have to use local storage or cookies. I was disheartened by the answer here so I found my own solution.

Use a boolean and an embedded if statement.

var alerted = false;

if (x > y)
{
     y = z;
     if (alerted === false)
     {
          alert("show your message");
          alerted = true;
     }
}

You can then reset the alerted variable later on if you want to show the alert message again.

like image 1
Anson Avatar answered Nov 06 '22 05:11

Anson