js获取当前时间并实时刷新1


显示代码

{{$moment(nowTime).format("YYYY-MM-DD HH:mm:ss")}} {{nowWeek}}

 data() {
    return {
      nowTime: new Date(),
      weekDays: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
      timer: null
    };
  },
  mounted() {
    // 初始化时间
    this.init();
  },
  destroyed() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  },
  computed: {
    nowWeek() {
      return (this.nowTime && this.weekDays[this.nowTime.getDay()]) || "";
    }
  },
  methods: {
    init() {
      this.startTimer();
    },
    // 启动定时器,定时刷新时间
    startTimer() {
      this.nowTime = new Date();
      setTimeout(() => {
        this.nowTime = new Date();
        this.timer = setInterval(() => {
          this.nowTime = new Date();
        }, 1000);
      }, 1000 - this.nowTime.getMilliseconds());
    }
  }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM