当前位置:网站首页>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.
边栏推荐
猜你喜欢
461. 汉明距离
- [email protected] 610/[email protected] 594/Alexa 56"/>
染料修饰核酸RNA|[email protected] 610/[email protected] 594/Alexa 56
Often forget HiFlow 】 【 check-in?Use tencent cloud scenario connector to remind you every day.
华为设备配置VRRP负载分担
手动输入班级人数及成绩求总成绩和平均成绩?
Edge box + time series database, technology selection behind Midea's digital platform iBuilding
为什么 BI 软件都搞不定关联分析
力扣203-移除链表元素——链表
Several difficult problems in DDD
chartjs自定义柱状图插件
随机推荐
尚医通项目总结
染料修饰核酸RNA|[email protected] 610/[email protected] 594/Alexa 56
Auto.js实现朋友圈自动点赞
直播平台怎么搭建,针对输入框的各种组件
Interesting opencv - record image binarization and similarity
error: C1083: 无法打开包括文件: “QString”: No such error: ‘QDir‘ file not found
算法--交错字符串(Kotlin)
abs()、fabs() 和 labs() 的区别
盲埋孔PCB叠孔设计的利与弊
函数,递归以及dom简单操作
用 setTimeout 来实现 setInterval
Abs (), fabs () and LABS ()
ES6 - Arrow Functions
李沐动手学深度学习V2-自然语言推断与数据集SNLI和代码实现
在树莓派上搭建属于自己的网页(3)
2022.8.2
ES6--residual parameters
NAACL 2022 | 具有元重加权的鲁棒自增强命名实体识别技术
leetcode 16. 数值的整数次方(快速幂+递归/迭代)
ES6-箭头函数