组件里的定时器无法销毁
发布于 3 年前 作者 clubadmin 1060 次浏览 来自 问答
粉丝福利 : 关注VUE中文社区公众号,回复视频领取粉丝福利
 destroyed() {
        window.clearInterval(this.timeInterval)
        this.timeInterval = null;
    },
    beforeDestroy() {
        window.clearInterval(this.timeInterval)
        this.timeInterval = null;
    },
    methods: {
        start() {
            let that = this
            

            this.timeInterval = setInterval(()=> {
                let lis = document.getElementById("creditRoll").getElementsByTagName("li")
                if(that.index < lis.length-1) {
                    that.keyframes('creditRoll'+that.index, (that.index)*28*-1, (that.index+1)*28*-1)
                    if(!document.getElementById("creditRoll")) {
                        this.$once('hook:beforeDestroy', () => {            
                            window.clearInterval(that.timeInterval)                         
                        })
                    }
                    document.getElementById("creditRoll").style = 
                    ` animation-duration: 1.7s; 
                    animation-timing-function: ease-in;
                    animation-iteration-count: 1;
                    animation-name: creditRoll${that.index} ;
                    transform: translateY(-${(that.index+1)*28}px)`;

                    that.index ++
                }
                if(that.index == lis.length-1) {
                    that.index = -1
                }
                
            },this.intervalTime)
            
        },
回到顶部