在vue使用定时器,定时器清不掉应当手动清除
<template>
<div>{{count}}</div>
</template>
<script>
export default {
data() {
return {
count: 2,
timer: null
};
},
mounted() {
this.timer = setInterval(this.add, 1000);
},
methods: {
add() {
this.count++;
console.log(this.count);
},
},
beforeDestroy() {
clearInterval(this.timer);
this.timer = null;
},
};
</script>