美文网首页
通过 $on 或 $once 监听页面生命周期销毁来去掉定时器,

通过 $on 或 $once 监听页面生命周期销毁来去掉定时器,

作者: 阿克兰 | 来源:发表于2020-08-05 22:51 被阅读0次

export default {

    mounted() {

        this.creatInterval('hello')

        this.creatInterval('world')

    },

    creatInterval(msg) {

        let timer = setInterval(() => {

            console.log(msg)

        }, 1000)

        this.$once('hook:beforeDestroy', function() {

            clearInterval(timer)

        })

    }

}

相关文章

网友评论

      本文标题:通过 $on 或 $once 监听页面生命周期销毁来去掉定时器,

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