Vue 定时执行函数

 
例一、
<script>
        new Vue({
            el: '#app',
            data() {
                return {
                    clock: '',
                }
            },
            mounted() {
                this.$nextTick(() => {
                    setInterval(this.CurentTime, 1000);
                })
            },
            methods: {
                CurentTime() {
                    var getTime = new Date();
                    var year = getTime.getFullYear(); //年
                    var month = getTime.getMonth() + 1; //月
                    var day = getTime.getDate(); //日
                    var hh = getTime.getHours(); //时
                    var mm = getTime.getMinutes(); //分
                    var ss = getTime.getSeconds(); //秒
                    var clock = year + "-";
                    if (month < 10)
                        clock += "0";
                    clock += month + "-";

                    if (day < 10)
                        clock += "0";

                    clock += day + " ";

                    if (hh < 10)
                        clock += "0";

                    clock += hh + ":";

                    if (mm < 10) clock += '0';
                    clock += mm + ":";

                    if (ss < 10) clock += '0';
                    clock += ss;

                    this.clock = clock
                }

            },
        })
    </script>

例二、
 var app = new Vue({         el: '#app',         data: {                      },         filters: {                   },         created: function () {             setInterval(this.timer, 1000);         },         methods: {          timer: function () {                 console.log("time");             }         },         watch: {         },         computed: {         }     }); 

作者: 执着小钟

执着小钟

发表评论