当前位置:网站首页>定时器的类型
定时器的类型
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);
可以看到的是执行了三次,就不在执行了,此时已经被清除了。
要清除定时器,首先要给它个名字。并最后清除它。
边栏推荐
- 小试牛刀:Go 反射帮我把 Excel 转成 Struct
- 梅克尔工作室-第一次
- ES6 类
- 英文语法-时与态
- The role of /etc/profile, /etc/bashrc, ~/.bash_profile, ~/.bashrc files
- 名创优品斥资6.95亿购买创始人叶国富所持办公楼股权
- MANIFEST.MF文件(PDB文件)
- sentinel与nacos持久化
- 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
- Recommendation System - Recall Phase - 2013: DSSM (Twin Towers Model) [Embedding (Semantic Vector) Recall] [Microsoft]
猜你喜欢
随机推荐
Selenium自动化中无头浏览器的应用
学习笔记12--路径-速度分解法之局部路径搜索
易驱线主控芯片对比(电动三轮电机90O瓦世纪通达)
Description of Hikvision camera streaming RTSP address rules
Ubantu project 4: xshell, XFTP connected the virtual machine and set xshell copy and paste the shortcut
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与分布式:主从复制
基于极限学习机(ELM)进行多变量用电量预测(Matlab代码实现)
乡村基冲刺港交所:5个月期内亏2224万 SIG与红杉中国是股东
How to clean up the lodash.memoize cache in the element-plus virtual table virtual-list component?
R语言的画图代码及差异性分析[通俗易懂]
Groupid(artifact id)
大健云仓冲刺美股:增营收反减利润 京东与DCM是股东
SetoolKit User Guide
NPM Taobao mirror (latest version) released a new version of npm mirror at 2021-11-21 16:53:52 [easy to understand]
TRACE32 - SNOOPer-based variable logging
力扣:714. 买卖股票的最佳时机含手续费
OAuth2:搭建授权服务器
Recommendation System - Recall Phase - 2013: DSSM (Twin Towers Model) [Embedding (Semantic Vector) Recall] [Microsoft]
LeetCode二叉树系列——226.翻转二叉树