当前位置:网站首页>定时器的类型
定时器的类型
2022-07-31 15:03:00 【这次我一定要赢】
一.定时器 setTimeout,setInterval
(1) setTimeout
setTimeout(() => {
console.log('我到时间了该执行我了');
}, timeout);
这个定时器只会去执行一次
代码:
setTimeout(() => {
console.log('我到时间了该执行我了');
}, 2000);

可以看到过了2s打了里面的内容。
请继续往下看
(2) setInterval
setInterval(() => {
console.log('我到时间了该执行我了');
}, interval);代码:
setInterval(() => {
console.log('我到时间了该执行我了');
}, 2000);
每过2s就会去执行一次。
二.清除定时器clearTimeout
(1) clearTimeout(定时器的名字)
let timer = setTimeout(() => {
console.log('我到时间了该执行我了');
}, 2000);
clearTimeout(timer)可以看到的是控制台里面并没有打印什么。
(2)clearInterval(定时器的名字)
let timer1 = setInterval(() => {
console.log('我到时间了该执行我了');
}, 2000);
setTimeout(() => {
clearInterval(timer1)
}, 6000);

可以看到的是执行了三次,就不在执行了,此时已经被清除了。
要清除定时器,首先要给它个名字。并最后清除它。
边栏推荐
- TRACE32——常用操作
- TRACE32 - SNOOPer-based variable logging
- Trigonometric identity transformation formula
- Ubantu project 4: xshell, XFTP connected the virtual machine and set xshell copy and paste the shortcut
- OAuth2:使用JWT令牌
- Web自动化实战——Selenium4(自动化测试环境的搭建)
- OpenShift 4 - Customize RHACS security policies to prevent production clusters from using high-risk registry
- Gorm—Go language database framework
- STM32(十)------- SPI通信
- C language basic practice (nine-nine multiplication table) and printing different asterisk patterns
猜你喜欢

Sentinel限流和异常处理

看交互设计如何集成到Scrum敏捷流程中

Five dimensions to start MySQL optimization

RecyclerView的高效使用第一节

UnityShader入门学习(三)——Unity的Shader
![[CUDA study notes] First acquaintance with CUDA](/img/a2/f322ebe9dc483028f68882ee2c866b.png)
[CUDA study notes] First acquaintance with CUDA

Getting started with UnityShader (3) - Unity's Shader

学习笔记12--路径-速度分解法之局部路径搜索

OAuth2:使用JWT令牌

Synchronized and volatile interview brief summary
随机推荐
Sentinel流量控制
R language test whether the sample conforms to normality (test whether the sample comes from a normally distributed population): shapiro.test function tests whether the sample conforms to the normal d
Excel快速对齐表格的中姓名(两个字姓名和三个字姓名对齐)
英文语法-时与态
Destruction order of thread_local variables
自适应控制——仿真实验三 用超稳定性理论设计模型参考自适应系统
OpenShift 4 - 定制 RHACS 安全策略,阻断生产集群使用高风险 Registry
华医网冲刺港股:5个月亏2976万 红杉与姚文彬是股东
力扣:714. 买卖股票的最佳时机含手续费
Spark学习(3)-Spark环境搭建-Standalone
大健云仓冲刺美股:增营收反减利润 京东与DCM是股东
《微信小程序-进阶篇》Lin-ui组件库源码分析-Icon组件
Ubantu专题4:xshell、xftp连接接虚拟机以及设置xshell复制粘贴快捷键
RecyclerView的高效使用第一节
"Listen to me, thank you" can be said in ancient poetry?Tsinghua University has developed an artifact of "Searching Sentences According to Meaning", which can search for the famous sayings you want wi
格林美瑞交所IPO:募资3.8亿美元 更多中国企业将赴欧洲上市
NPM淘宝镜像(最新版本)于2021-11-21 16:53:52发布新版本npm镜像[通俗易懂]
OpenShift 4 - Deploy Redis Cluster with Operator
ERROR: Failed building wheel for osgeo
2021 OWASP TOP 10 漏洞指南