鍍金池/ 問答/數(shù)據(jù)庫  HTML/ vue中,在methods里的一個(gè)function里有個(gè)定時(shí)器,在另一個(gè)func

vue中,在methods里的一個(gè)function里有個(gè)定時(shí)器,在另一個(gè)function里如何清除它

在組件里,在一個(gè)function里設(shè)全局的timer=Interval(),在另一方法里找不到。如何跨function清除

回答
編輯回答
萢萢糖

data里寫個(gè)變量timer, 定義定時(shí)器的時(shí)候 this.timer = setInterval()
在其他function里, 用clearInterval(this.timer)清除即可

2017年4月2日 21:02
編輯回答
老梗

在data里聲明一個(gè)timer
data() {
return {

timer: undefined

}
},
methods: {
a() {
this.timer = setInterval(()=>{},1000)
},
b() {
clearInterval(this.timer)
}

2018年5月31日 18:23
編輯回答
萌二代

把定時(shí)器綁定在this下面

2017年5月26日 22:00