Goal is to remove local storage item after 24 hours, meaning show modal only once per day everyday. The local storage value is set. However, my condition to check if local storage item does not exist and 24 hours have passed then show modal is only half way working. I need it to expire the modal (not show) if the local storage value has been set for less than 24 hours. Currently, it is not removing the local storage item and showing the modal only once on load. If refresh page after time frame the local storage item is still there. Using Sweet Alert for modal, but that should not matter I don’t think. Also not sure if I need a for loop or set time interval for this.
To make testing easier for a one minute duration you can also use:
expires = expires.setMinutes(expires.getMinutes() + 1);
Here is what I tried:
$(document).ready(function() {
if (window.localStorage) {
var currentUser = '<%= current_user %>';
var swal_alert = localStorage.getItem('alert');
var currentTime = new Date($.now());
var expires = new Date();
expires = expires.setHours(expires.getHours() + 24);
if (currentUser && swal_alert !=1 && currentTime < expires) {
swal ({
icon: 'warning',
text: 'Notice text here.',
closeOnClickOutside: false,
button: 'ok'
})
} else {
localStorage.removeItem('alert');
}
localStorage.setItem('alert', '1');
}
}