浅谈vue 组件中的setInterval方法和window的不同
网络编程 2021-07-04 14:06www.168986.cn编程入门
这篇文章主要介绍了浅谈vue 组件中的setInterval方法和window的不同,具有很好的参考价值,希望对大家有所帮助。一起跟随长沙网络推广过来看看吧
vue组件中,this指向实例,【实例中重写了setInterval等一整套方法】。所以,千万不能和 window 下挂载的方法混用
具体不同在于,window.setInterval执行完比后返回一个id,而vue实例中返回【定时器对象】,当然该对象中包含一个_id的私有属性
因为 clearInterval 方法参数是id,所以最佳实践是统一使用 window 的方法,不要使用 vue组件的方法
vue中的定时器方法,要使用箭头函数,不要出现 const that = this 的写法
//正确的用法 mounted() { // 如果不加 window ,则会使用 vue实例的方法,将无法清除定时器 this.timer = window.setInterval(() => { this.date = new Date(); }, 2000); console.log(this.timer);//number }, methods: { clearTimer() { window.clearInterval(this.timer); this.timer = null; } }
补充知识:vue 切换页面 setInterval
vue 是单页面应用,路由切换后,定时器并不会自动关闭,需要手动清除,当页面被销毁时,清除定时器即可。
mounted(){ clearInterval(this.timer); this.setTimer(); }, destroyed(){ clearInterval(this.timer) }
以上这篇浅谈vue 组件中的setInterval方法和window的不同就是长沙网络推广分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程