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

可以看到的是执行了三次,就不在执行了,此时已经被清除了。
要清除定时器,首先要给它个名字。并最后清除它。
边栏推荐
- NC | 中国农大草业学院杨高文组揭示发现多因子干扰会降低土壤微生物多样性的积极效应...
- The meaning of node_exporter performance monitoring information collection in Prometheus
- 看交互设计如何集成到Scrum敏捷流程中
- R language moves time series data forward or backward (custom lag or lead period): use the lag function in the dplyr package to move the time series data forward by one day (set the parameter n to a p
- Redis与分布式:集群搭建
- 49. The copy constructor and overloaded 】
- OpenShift 4 - 定制 RHACS 安全策略,阻断生产集群使用高风险 Registry
- Gorm—Go语言数据库框架
- 最小费用最大流问题详解
- R language ggplot2 visualization: use the ggmapplot function of the ggpubr package to visualize the MA plot (MA-plot), the font.legend parameter and the font.main parameter to set the title and legend
猜你喜欢

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

基于最小二乘法和SVM从天气预报中预测太阳能发电量(Matlab代码实现)

Public Key Retrieval is not allowed error solution when DBeaver connects to MySQL 8.x

格林美瑞交所IPO:募资3.8亿美元 更多中国企业将赴欧洲上市

Female service community product design

Asynchronous processing business using CompletableFuture

Prometheus之node_exporter性能监控信息采集含义

OpenShift 4 - Deploy Redis Cluster with Operator

Ubantu专题4:xshell、xftp连接接虚拟机以及设置xshell复制粘贴快捷键

公告
随机推荐
Prometheus之node_exporter性能监控信息采集含义
Getting started with UnityShader (3) - Unity's Shader
自适应控制——仿真实验三 用超稳定性理论设计模型参考自适应系统
OpenShift 4 - Deploy Redis Cluster with Operator
TRACE32——C源码关联
SQL、HQL、JPQL 到底有什么区别
什么是消息队列呢?
Trigonometric identity transformation formula
梅克尔工作室-第一次
Spark学习(2)-Spark环境搭建-Local
基于最小二乘法和SVM从天气预报中预测太阳能发电量(Matlab代码实现)
Ubantu专题4:xshell、xftp连接接虚拟机以及设置xshell复制粘贴快捷键
TCP详解
Destruction order of thread_local variables
Ubantu专题5:设置静态ip地址
Advanced Mathematics - Commonly Used Indefinite Integral Formulas
49. The copy constructor and overloaded 】
RecyclerView高效使用第三节
OpenCV测量物体的尺寸技能 get~
OAuth2:微服务权限校验Session共享