当前位置:网站首页>26. 定时器
26. 定时器
2022-06-11 19:54:00 【Suyuoa】
目录
3 停止setTimeout()定时器 clearTimeout()
6 停止setInterval()定时器 clearInterval()
1 只执行一次函数定时器 setTimeout()
语法是这样的,第二个参数可以省略,省略默认为0
![]()
window可以省略

打开页面后什么也没有出现

5s之后执行函数,自动执行,不需要进行其他操作

每一次刷新后会重置时间
写函数名效果一样


也可以这样写,效果也一样,一般我们不这样写


一个页面中可能会有很多的定时器,一般我们会把定时器放到一个变量中
![]()
定时器中使用的函数也被称为回调函数,我们之前注册时间使用的函数也可以被称作回调函数
2 五秒后关闭广告
广告内容我们直接用div替代了

打开后有一个盒子

五秒之后盒子消失

3 停止setTimeout()定时器 clearTimeout()
语法是这样的
![]()
我们现在随便按一个键,按键之后让广告不消失

这里注意一定将clearTimeout()套在function中,不要直接使用
打开页面后有这个广告

在消失之前我们随便按一个键,按下后发现广告一直存在

4 循环执行函数定时器 setInterval()
语法是这样的,使用方式与setTimeout()相似
![]()
毫秒数可省略,如果省略毫秒数就为0

这样每过一秒就会执行一遍回调函数

5 倒计时效果

这里注意在使用setInterval()前需要首先执行一遍回调函数,这样就没有一开始晚一秒出现的效果了
每隔一秒页面会自动刷新

有时会出现两秒两秒跳的情况,有时精度没有这么高,没法避免
如果要指定某年某月,那么修改target_time就可以了


6 停止setInterval()定时器 clearInterval()
语法是这样的
![]()

当我们按下按键的时候计时器就会停止

7 定时器的启停
如果使用setTimeout()停了之后是不能再启动的,如果使用setInterval()停了之后只需要再执行一遍setInterval()就可以再启动定时器

这里注意开始按钮中定义timer要定义为全局变量
点击开始定时器按钮会开始循环执行回调函数,点击关闭定时器按钮会停止循环执行回调函数

8 发送短信案例
打开页面后是这样的

点击发送后会让按钮处于不可用状态5s,并显示还有几秒恢复为可用状态

5s后恢复可用状态,再次点击依然重复上面的功能

代码是是这样的

边栏推荐
- In 2021, the global revenue of Indian fragrant rice was about $12530 million, and it is expected to reach $21670 million in 2028
- Golang学习笔记—基础篇
- Raki's notes on reading paper: learning fast, learning slow: a general continuous learning method
- 浅聊对比学习(Contrastive Learning)第一弹
- 干货!基于序列超图神经网络的信息扩散预测
- Introduction to typescript
- unity里面使用opencv,qt建立plugin(一)
- 【Bug解决】UnpicklingError: A load persistent id instruction was encountered, but no persistent_load.
- Flutter doctor 显示xcode没有安装的解决办法
- 图像变换 torchvision.transforms 笔记
猜你喜欢

In 2021, the global barite product revenue was about $571.3 million, and it is expected to reach $710.2 million in 2028

Tensorflow --- creation and reading of tfrecord file

Calculate the number of 926 in the string to the power of 9260 of 926

全名单!2022泰晤士亚洲大学排名公布!

【四川大学】初试复试考研资料分享

Yolov3 pytoch code and principle analysis (I): runthrough code

Flutter doctor 顯示xcode沒有安裝的解决辦法

APB2standard_ handshake_ Bridge design

Shanghai internal promotion 𞓜 Yuyang teacher's research group of Shanghai Chizhi research institute recruits full-time researchers

VS2010 cannot open when linking sql2008 database
随机推荐
These postgraduate entrance examination majors are easy to be confused. If you make a mistake, you will take the exam in vain!
Comp3411 -prolog language
Yolov3 pytoch code and principle analysis (I): runthrough code
Raki's notes on reading paper: learning fast, learning slow: a general continuous learning method
[help] how can wechat official account articles be opened in an external browser to display the selected messages below?
LNMP架构源码编译安装图文讲解(附带搭建论坛实验)
Proficient in xmake
基于 Vue + Codemirror 实现 SQL 在线编辑器
AHB2Standard_ handshake_ Bridge design
cocan yocto buildroot
Building web applications
This article introduces you to the infrastructure of sofaregistry
[laravel series 7.5] event system
上海内推 | 上海期智研究院于洋老师课题组招收全职研究员
Golang学习笔记—基础篇
Hdu3527 (Hangdian) spy problem
POJ 1458 longest common subsequence (dynamic planning exercise)
AHB_ Bus_ Matrix_ 3x3 design
Skywalking source code analysis Part 5 - server configuration configuration module startup
Multimodal learning toolkit paddlemm based on propeller