JSFiddle - React, Tailwind, and code Playground
HTML
<html>
<head>
<script type='text/javascript'>
function has24HourPassed(key) {
var storeval = localStorage.getItem(key);
if (!storeval) return true; /* nothing stored in storage */
var T24_HOUR = 24 * 60 * 60 * 1000;
if ((new Date - new Date(storeval)) < T24_HOUR) return false;
localStorage.removeItem(key); /* 24 hours passed so we can remove stored date */
return true;
}
(function(d) {
if (has24HourPassed('al-news')) {
d.classList.add('al-news');
}
if (has24HourPassed('al-maintenance')) {
d.classList.add('al-maintenance');
}
})(document.documentElement);
</script>
</head>
<body>
<article class="alert-box" id="alert-box-news">
<h1>News Alerts</h1>
<p>text text text text text text text text text text text text text text</p>
<a class="alert-box-close" id="close-alert-box-news">
<img src="http://i.imgur.com/czf8yas.png" height="25" width="25" alt="">
</a>
</article>
<article class="alert-box" id="alert-box-maintenance">
<h1>Site Maintenance</h1>
<p>text text text text text text text text text text text text text text</p>
<a class="alert-box-close" id="close-alert-box-maintenance">
<img src="http://i.imgur.com/czf8yas.png" height="25" width="25" alt="">
</a>
</article>
<button>For this demo - to clear local storage</button>
</body>
</html>
CSS
.alert-box {
display: none;
width: 50vw;
position: relative;
margin: 20px auto;
border: 1px solid black;
}
.alert-box-close {
position: absolute;
top: -12px;
right: -12px;
cursor: pointer;
}
.al-news #alert-box-news,
.al-maintenance #alert-box-maintenance {
display: block;
}
JavaScript
var alerts = document.querySelectorAll('.alert-box-close');
for (var i = 0; i < alerts.length; i++) {
alerts[i].addEventListener('click', function(e) {
var boxtype = e.target.parentElement.id.split('-').pop();
document.getElementById('alert-box-' + boxtype).style.display = "none";
localStorage.setItem("al-" + boxtype, new Date());
})
}
document.querySelector('button').addEventListener('click', function(e) {
localStorage.clear();
})