vue离开页面清除定时器

<script>
    export default {
        name:'test',
        data(){
            return {
                time: 60,
                timer:null,
            }
        }, 

        methods: {
            // 60s倒计时
            getLastTime: function(){
                this.timer = setInterval(function () {
                    if(this.time == 0){
                        this.time = 60;
                        clearInterval(this.timer);
                    }else {
                        this.time = this.time - 1;
                    }
                },1000) 
            },
        },
        destroyed(){
            if(this.timer) { //如果定时器在运行则关闭
                clearInterval(this.timer); 
            }
        }
    }
</script>

猜你喜欢

转载自blog.csdn.net/qq_39364032/article/details/83150052