在vue开发使用定时器切记要销毁定时器,否则后期会造成不可预测的bug
const app = new Vue({
el:"#app",
data:{
display:'block',
n:3,
timer:null
},
methods:{
autoplay(){
this.timer=setInterval(this.play,1000)
},
play(){
this.n--
if(this.n==0){
this.display='none',
clearInterval(this.timer)
}
},
jump(){
this.display="none"
clearInterval(this.timer)
}
},
//生命周期
mounted(){
this.autoplay()
},
destroyed(){
clearInterval(this.timer)
}//切记要销毁
})