定时器以及销毁

利用hook监听钩子函数  进行销毁 这种一般倒销毁倒计时用的比较多 
hook还可以监听组件的钩子函数
mounted () {
    this.timer = setInterval(() => {
      this.getMyDate()
    }, 1000);
    this.$on('hook:destroyed', () => { clearInterval(this.timer); });
  },



  // mounted () {
  //   this.timer = setInterval(() => {this.getMyDate();}, 1000);
  // },
  // destroyed () {
  //   clearInterval(this.timer);
  // },
  methods: {
    getMyDate () {
      var _this = this;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      // let week = new Date().getDay();
      let week = '星期' + '日一二三四五六'.charAt(new Date().getDay());
      let hh = new Date().getHours();
      let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
      let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
      _this.gettime = yy + '/' + mm + '/' + dd + ' ' + week + ' ' + hh + ':' + mf + ':' + ss;
    }
posted @ 2021-10-18 17:06  夏天的博客元  阅读(208)  评论(0)    收藏  举报