vue离开页面清除定时器
<script>export default {name:'test',data(){return {time: 60,timer:null,}},methods: {...
·
<script>
export default {
name:'test',
data(){
return {
time: 60,
timer:null,
}
},
methods: {
// 60s倒计时
getLastTime: function(){
this.timer = setInterval(function () {
if(this.time == 0){
this.time = 60;
clearInterval(this.timer);
}else {
this.time = this.time - 1;
}
},1000)
},
},
destroyed(){
if(this.timer) { //如果定时器在运行则关闭
clearInterval(this.timer);
}
}
}
</script>
更多推荐
已为社区贡献5条内容
所有评论(0)