美文网首页
vue获取当前时间 实时刷新

vue获取当前时间 实时刷新

作者: 程序媛_ | 来源:发表于2021-09-06 10:42 被阅读0次

https://www.cnblogs.com/lidonglin/p/11362384.html

export default {

data() {

return {

timer: "",//定义一个定时器的变量

      currentTime: new Date(), // 获取当前时间

}

},

methods: {

},

created() {

var _this = this; //声明一个变量指向Vue实例this,保证作用域一致

    this.timer = setInterval(function() {

      _this.currentTime = //修改数据date

        new Date().getFullYear() +

        "-" +

        (new Date().getMonth() + 1) +

        "-" +

        new Date().getDate() +

        " " +

        new Date().getHours() +

        ":" +

        new Date().getMinutes() +

        ": " +

        new Date().getSeconds();

    }, 1000);

},

beforeDestroy() {

    if (this.timer) {

      clearInterval(this.timer); // 在Vue实例销毁前,清除我们的定时器

    }

  }

}

相关文章

网友评论

      本文标题:vue获取当前时间 实时刷新

      本文链接:https://www.haomeiwen.com/subject/ofsewltx.html