zoukankan      html  css  js  c++  java
  • 实时显示时间

    让页面一开始就加载就显示时间。
    解决隔1s后才显示时间这个问题

    clearTime的值 不等于空时,说明已经开启定时器了哈。
    

    <div id="app">
        {{Showtime}}
    </div>
    
    <script>
        var vm = new Vue({
            el: "#app",
            data: {
                Showtime:"",
                clearTime:null, 
            },
    
            created() {
                // 让页面一开始就加载就显示时间。
                //解决隔1s后才显示时间这个问题
                let newtime = new Date();
                let myHover = newtime.getHours();
                let myMin = newtime.getMinutes();
                this.Showtime = myHover + ":" + myMin;
    
                // 让后每隔1s跟新时间
                this.runTime();
            },
            methods: {
                runTime() {
                    this.clearTime = setInterval(() => {
                        let newtime = new Date();
                        let myHover = newtime.getHours();
                        let myMin = newtime.getMinutes();
                        this.Showtime = myHover + ":" + myMin;
                    }, 1000)
                }
            },
            //离开页面清除定时器
            beforeDestroy(){
               clearInterval( this.clearTime);
               console.log("---")
            }  
    
        })
    </script>
    
  • 相关阅读:
    camp待补
    ZOJ
    ZOJ
    ZOJ
    CodeForces
    CodeForces
    POJ 3278 Catch That Cow(简单BFS)
    POJ 2251 Dungeon Master(三维BFS)
    POJ 1218 THE DRUNK JAILER(类开灯问题,完全平方数)
    HDU 2053 Switch Game(开灯问题,完全平方数)
  • 原文地址:https://www.cnblogs.com/IwishIcould/p/12199125.html
Copyright © 2011-2022 走看看