在mounted中创建并执行定时器,然后在beforeDestroy或者destroyed中清除定时器
<template>
<div class="about">
</div>
</template>
<script>
export default { name: "about",
data() { return { //接收定时器
timer: ""
};
},
mounted() { let _this = this; let num = 0; //创建并执行定时器
this.timer = setInterval(() => { //当num等于100时清除定时器
if (num == 100) {
clearInterval(_this.timer);
} console.log(num++);
}, 1000);
},
beforeDestroy() { //清除定时器
clearInterval(this.timer); console.log("beforeDestroy");
},
destroyed() { //清除定时器
//clearInterval(this.timer);
console.log("destroyed");
}
}; </script>
<style scoped>
</style>
作者:神仙哥哥卿洋
链接:https://www.jianshu.com/p/aa95c8aefbf5
点击查看更多内容
2人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦