javascript - 如何让vue每天只显示一次弹出窗口

标签 javascript typescript vue.js

我有一个方法,可以在用户登录网站 4 秒后显示模式窗口。如果关闭,则在接下来的 24 小时内再次显示。我有一些问题。 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)
      }
    }
  }

最佳答案

在您的 setDevHoursModal 方法中,您并不总是返回值。您可以通过返回条件结果来解决此问题:

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;
}

关于javascript - 如何让vue每天只显示一次弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59037829/

相关文章:

javascript - 通过 AJAX 调用问题 knockout 自定义验证

angular - angular2 中 md-checkbox 的状态

typescript - Clasp - 在 Typescript 中使用环境变量 secret 进行 Google Apps 脚本开发

vue.js - 自定义处理 vue 路由器 ID 中的正斜杠

javascript - Angular2 和 Karma 仅运行一个测试套件,又称为“describe.only”

javascript - promise 链中基于 NodeJS 回调函数的执行顺序错误

javascript 复选框 - 仅在 Bootstrap 模式下取消选中 OK

types - 创建一个不能被基类型替代的新类型

modal-dialog - 使用 vue.js 2.0 打开引导模式

javascript - Vue 2 和 Vue-Resource [无法读取 undefined(...) 的属性 'get']