[英]How to display a popup only once per day vue
I have a method that shows a modal window 4 seconds after the user logs on to the site.我有一种方法可以在用户登录网站 4 秒后显示一个模态窗口。 If it is closed then show again next 24 hours.如果它已关闭,则在接下来的 24 小时内再次显示。 I have some problems.我有一些问题。 LocalStorage does not store my data and does not return a variable that stores the date, how can I fix it? LocalStorage 不存储我的数据,也不返回存储日期的变量,我该如何解决?
mounted () {
this.runModalTimer()
this.setDevHoursModal()
},
methods: {
runModalTimer (): void {
setTimeout(() => {
this.isModalVisible = true
}, 4000)
},
setDevHoursModal (): boolean {
if (localStorage) {
let nextPopup = localStorage.getItem('isModalVisible')
if (nextPopup > new Date()) {
return this.isModalVisible = true
}
let expires = new Date()
expires = expires.setHours(expires.getHours() + 24)
localStorage.setItem('isModalVisible', expires)
}
}
}
In your setDevHoursModal
method, you're not always returning a value.在您的setDevHoursModal
方法中,您并不总是返回一个值。 You could resolve this by just returning the result of the condition:您可以通过仅返回条件的结果来解决此问题:
setDevHoursModal(): boolean {
if (localStorage) {
let nextPopup = localStorage.getItem('isModalVisible');
if (nextPopup > new Date()) {
return (this.isModalVisible = true); // let's be a little more explicit here
}
let expires = new Date();
expires = expires.setHours(expires.getHours() + 24);
localStorage.setItem('isModalVisible', expires);
return false;
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.