当前位置:网站首页>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.
边栏推荐
- chart.js多条曲线图插件
- leetcode 2119. 反转两次的数字
- leetcode 326. Powers of 3
- leetcode 2119. Numbers reversed twice
- leetcode 剑指 Offer 58 - II. 左旋转字符串
- Orcad Capture Cadence 新建原理图多部分smybol和Homogeneous、Heterogeneous类型介绍教程
- 有趣的opencv-记录图片二值化和相似度实现
- glusterfs 搭建使用
- 15年软件架构师经验总结:在ML领域,初学者踩过的五个坑
- Zero trust, which has been popular for more than ten years, why can't it be implemented?
猜你喜欢

详解虚拟机!京东大佬出品 HotSpot VM 源码剖析笔记(附完整源码)
[email protected] 610/[email protected] 594/Alexa 56"/>染料修饰核酸RNA|[email protected] 610/[email protected] 594/Alexa 56
[email protected] 594/[email prote"/>RNA核糖核酸修饰Alexa 568/[email protected] 594/[email prote

RNA核糖核酸修饰RNA-HiLyte FluorTM 405荧光染料|RNA-HiLyte FluorTM 405

检测和控制影子IT的五个步骤

有趣的opencv-记录图片二值化和相似度实现

3种圆形按钮悬浮和点击事件

svg+js订单确认按钮动画js特效

CheckBox列表项选中动画js特效

如何使用 Jmeter获取登录token并设置为全局变量?
随机推荐
RNA核糖核酸修饰荧光染料|HiLyte Fluor 488/555/594/647/680/750标记RNA核糖核酸
YARN功能介绍、交互流程及调度策略
2022 年值得尝试的 7 个 MQTT 客户端工具
EasyCVR平台海康摄像头语音对讲功能配置的3个注意事项
Li Mu hands-on learning deep learning V2-BERT fine-tuning and code implementation
华为设备配置VRRP与BFD联动实现快速切换
Go语言类型与接口的关系
华为设备VRRP配置命令
leetcode 136. Numbers that appear only once (XOR!!)
canvas螺旋动画js特效
华为设备配置VRRP负载分担
chart.js多条曲线图插件
数据库定时备份winserver2012篇
Zero trust, which has been popular for more than ten years, why can't it be implemented?
检测和控制影子IT的五个步骤
Likou 707 - Design Linked List - Linked List
if _name_ == “__main__“:NameError: name ‘_name_‘ is not defined
直播平台怎么搭建,针对输入框的各种组件
tRNA-m5C转运RNA(tRNA)修饰5-甲基胞嘧啶(m5C)|tRNA修饰m1Am2A (2-methyladenosine)
极验深知v2分析