当前位置:网站首页>定时器的类型
定时器的类型
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);

可以看到的是执行了三次,就不在执行了,此时已经被清除了。
要清除定时器,首先要给它个名字。并最后清除它。
边栏推荐
- AVH Deployment Practice (1) | Deploying the Flying Paddle Model on Arm Virtual Hardware
- WeChat chat record search in a red envelope
- Spark学习(2)-Spark环境搭建-Local
- Getting started with UnityShader (1) - GPU and Shader
- 网线RJ45接口针脚[通俗易懂]
- OAuth2:搭建授权服务器
- SQL、HQL、JPQL 到底有什么区别
- 【CUDA学习笔记】初识CUDA
- 力扣:56. 合并区间
- LeetCode二叉树系列——222.完全二叉树的节点个数
猜你喜欢

TRACE32 - Common Operations

Spark学习(2)-Spark环境搭建-Local

TRACE32——基于SNOOPer的变量记录

Efficient use of RecyclerView Section 1

工程水文学复习资料

Essential Learning for Getting Started with Unity Shader - Transparency Effect

Word table to Excel

大健云仓冲刺美股:增营收反减利润 京东与DCM是股东

使用 Chainlink Keepers 实现智能合约函数的自动化执行

基于极限学习机(ELM)进行多变量用电量预测(Matlab代码实现)
随机推荐
Groupid(artifact id)
UnityShader入门学习(三)——Unity的Shader
sentinel与nacos持久化
蔚来杯2022牛客暑期多校训练营4
How to grab configuration information for DELL SC compellent storage system
【CUDA学习笔记】初识CUDA
Gorm—Go语言数据库框架
Ubantu project 4: xshell, XFTP connected the virtual machine and set xshell copy and paste the shortcut
OpenShift 4 - 用 Operator 部署 Redis 集群
基于极限学习机(ELM)进行多变量用电量预测(Matlab代码实现)
Five dimensions to start MySQL optimization
Sentinel流量控制
最小费用最大流问题详解
[CUDA study notes] First acquaintance with CUDA
常用工具命令速查表
Introduction to BigDecimal, common methods
NC | 中国农大草业学院杨高文组揭示发现多因子干扰会降低土壤微生物多样性的积极效应...
R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化箱图、使用font函数自定义图例标题文本(legend.title)字体的大小、颜色、样式(粗体、斜体)
Efficient use of RecyclerView Section 2
Architecture actual combat battalion module 8 message queue table structure design