当前位置:网站首页>Use setTimeout to realize setInterval
Use setTimeout to realize setInterval
2022-08-03 20:49:00 【captain on board】
️ Author: Captain on board
Homepage:Visit Address Captain's Blog on Board
Introduction: Senior front-end development engineer, focusing on front-end development, welcome to consult and exchange, and learn together!Thanks:If you think the blogger's article is good or helpful to your work or solve your problem, you can follow and support the blogger.It will be better, and the bloggers here are grateful!!!If you have any questions, you can leave a message and comment, and you will reply in time after you see it.
(1) What is the difference between setInterval() and setInterval() with the setTimeout() method?
First of all, let's look at the defects of setInterval. The timer created with setInterval() ensures that the timer code is regularly inserted into the queue
.The problem is that if the timer code doesn't finish executing before the code is added to the queue again,
the result will be that the timer code runs several times in a row.And there is no space in between.But luckily: javascript
The engine is smart enough to avoid this problem.The
timer code will be added to the queue if and only if there is no how-to code instance for that timer.This ensures that the timer code joins the queue at the smallest time interval specified.
There are two problems with this rule of repeating timers: 1. Some intervals are skipped 2. The code execution time for multiple timers
may be smaller than expected.
Here's an example:
Suppose an onclick event handler uses setInterval() to set a repeating timer of 200ms.
function say(){setTimeout(say,200);};setTimeout(say,200);//orsetTimeout(function(){setTimeout(arguments.callee,200);},200);
WelcomeCaptain on board's blog, if you have any questions, you can leave a message or comment, and you will reply in time when you see it.
边栏推荐
- 尚医通项目总结
- 在树莓派上搭建属于自己的网页(3)
- RNA核糖核酸修饰Alexa 568/[email protected] 594/[email prote
- PyCharm函数自动添加注释无参数问题
- leetcode 231. Powers of 2
- RNA核糖核酸修饰RNA-HiLyte FluorTM 405荧光染料|RNA-HiLyte FluorTM 405
- Power button 206 - reverse list - the list
- Leetcode sword refers to Offer 15. 1 in the binary number
- ES6简介及let、var、const区别
- 2021年数据泄露成本报告解读
猜你喜欢
敏捷交付的工程效能治理
PyCharm函数自动添加注释无参数问题
火了十几年的零信任,为啥还不能落地
RNA核糖核酸修饰荧光染料|HiLyte Fluor 488/555/594/647/680/750标记RNA核糖核酸
检测和控制影子IT的五个步骤
Advantages and Disadvantages of Blind and Buried Via PCB Stacked Via Design
Zero trust, which has been popular for more than ten years, why can't it be implemented?
DDD 中的几个困难问题
tRNA甲基化偶联3-甲基胞嘧啶(m3C)|tRNA-m3C (3-methylcy- tidine)
【使用 Pytorch 实现入门级的人工神经网络】
随机推荐
LeetCode_位数统计_中等_400.第 N 位数字
leetcode 2119. Numbers reversed twice
error: C1083: 无法打开包括文件: “QString”: No such error: ‘QDir‘ file not found
leetcode 231. Powers of 2
15年软件架构师经验总结:在ML领域,初学者踩过的五个坑
博士申请 | 美国明尼苏达大学葛畅教授招收隐私数据管理方向全奖博士/硕士/博后/访问学者...
直播平台怎么搭建,针对输入框的各种组件
不专业面试官的经验总结
力扣203-移除链表元素——链表
ES6 - Arrow Functions
在树莓派上搭建属于自己的网页(3)
【HiFlow】经常忘记签到怎么办?使用腾讯云场景连接器每天提醒你。
leetcode 16.01. 交换数字(不使用临时变量交换2个数的值)
RNA核糖核酸修饰RNA-HiLyte FluorTM 405荧光染料|RNA-HiLyte FluorTM 405
svg+js订单确认按钮动画js特效
leetcode 072. Finding Square Roots
2022年1~7月语音合成(TTS)和语音识别(ASR)论文月报
极验深知v2分析
LitJson报错记录
分分钟教你读取 resources 目录下的文件路径