当前位置:网站首页>定时器(setInterval)的开启与关闭
定时器(setInterval)的开启与关闭
2022-08-10 05:31:00 【怎么吃不饱捏】
我设置的定时器是点击展示子组件的时候开启,等待30s后没有点击页面就关闭子组件,每次点击页面30s内没有点击也会关闭,当我点击退出子组件页面的时候也会关闭定时器
// 父组件展示子组件时开启定时器
this.$refs.manMachine.openOrCloseTime(true)
在子组件中定义定时器的名字以及用来计数的变量
// 开启定时器
openOrCloseTime(isOpen){
// 父组件调用方法时传参来决定是否开启定时器
if(isOpen==true){
timer=setInterval(()=>{
this.count--
// 当计数变量值变为0时关闭定时器
if(this.count==0){
this.close(timer);
}
},1000)
}else{
this.close(timer);
}
},
// 关闭定时器
close(timer){
clearInterval(timer);
// 把计数变量恢复初始值,以供下次使用
this.count=30;
// 修改父组件传过来的控制子组件是否展示的变量值,来关闭子组件
this.$emit("update:isChat", this.show);
},
在关闭页面的时候记得调用一下关闭定时器的方法
在对页面进行操作后,恢复计数变量初始值this.count=30,让定时器重新进行计时
边栏推荐
猜你喜欢
随机推荐
文章复现:超分辨率网络-VDSR
几种绘制时间线图的方法
pytorch框架学习(4)torchvision模块&训练一个简单的自己的CNN (一)
Mockito基本使用指南
你不知道的常规流
Pony语言学习(六):Struct, Type Alias, Type Expressions
【el和template区别】
ResNet的基础:残差块的原理
oracle rac 11g安装执行root.sh时报错
网络安全3
R绘制图像,图像特征提取
一文带你搞懂OAuth2.0
Conda creates a virtual environment method and pqi uses a domestic mirror source to install a third-party library method tutorial
GtkD开发之路
pytorch框架学习(6)训练一个简单的自己的CNN (三)细节篇
Attention candidates for the soft exam! The detailed registration process for the second half of 2022 is coming!
How does Jenkins play with interface automation testing?
SEO搜索引擎优化
接口文档进化图鉴,有些古早接口文档工具,你可能都没用过
k-近邻实现手写数字识别