I have implemented a dialogue which shows whenever a user access the website. However, I want the dialogue to reappear after 24 hours if someone accesses the site clicking on the cookie. Unfortunately, I spent a lot of time researching and I haven’t found any solution which applies to my scenario. Below is the code I’m trying to modify,
JavaScript
x
12
12
1
var is_dialogue = window.localStorage.getItem("dialogue");
2
if (is_dialogue != 'false') {
3
var today = new Date();
4
var hours = today.getHours();
5
dialogue = new Dialogue();
6
dialogue.setHtmlMessage(string('dialogue-heading'));
7
dialogue.addHtmlDetail(string('dialogue-detail'));
8
9
if (((today.getHours() + 24) >= 24) || ((today.getHours() + 24) <= 48))
10
localStorage.setItem("dialogue", "true");
11
}
12
Advertisement
Answer
localStorage has no expiration time, but you can compare timestamps:
JavaScript
1
16
16
1
function moreThanOneDayAgo(date) {
2
const DAY = 1000 * 60 * 60 * 24;
3
const dayAgo = Date.now() - DAY;
4
5
return date < dayAgo;
6
}
7
8
var is_dialogue = localStorage.getItem("dialogue");
9
if (is_dialogue === null || moreThanOneDayAgo(is_dialogue)) {
10
dialogue = new Dialogue();
11
dialogue.setHtmlMessage(string('dialogue-heading'));
12
dialogue.addHtmlDetail(string('dialogue-detail'));
13
14
localStorage.setItem("dialogue", Date.now());
15
}
16