美文网首页
vue 隔几秒自动请求接口 刷新页面

vue 隔几秒自动请求接口 刷新页面

作者: 匆白 | 来源:发表于2023-09-14 11:04 被阅读0次

初始化数据

data(){

    return {

        timer:null

    }

}

请求数据

mounted(){

    //刷新页面直接请求数据

    this.allGetMock();

    //之后3秒自动请求数据

    this.timer = setInterval(() => {

            this.allGetMock();

    },3000);

}

销毁定时器

beforeDestroy(){

    // 销毁定时器

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

        clearInterval(this.timer);

    })

}

相关文章

网友评论

      本文标题:vue 隔几秒自动请求接口 刷新页面

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