当前位置:网站首页>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.
边栏推荐
- 云服务器如何安全使用本地的AD/LDAP?
- EMQX Newsletter 2022-07|EMQX 5.0 正式发布、EMQX Cloud 新增 2 个数据库集成
- ES6 - Arrow Functions
- 数学之美 第六章——信息的度量和作用
- 函数,递归以及dom简单操作
- tkwebview2创作心得
- C51 存储类型与存储模式
- 2022/08/03 学习笔记 (day23)多线程(补充)
- Leetcode sword refers to Offer 15. 1 in the binary number
- leetcode refers to Offer 58 - II. Left Rotate String
猜你喜欢
随机推荐
leetcode 125. 验证回文串
系统运维系列 之CSV文件读取时内容中包含逗号的处理方法
if _name_ == “__main__“:NameError: name ‘_name_‘ is not defined
leetcode 1837. The sum of the digits in the K-base representation
力扣206-反转链表——链表
TweenMax.js向日葵表情变化
leetcode 2119. 反转两次的数字
Auto.js脚本程序打包
2022-8-3 第七组 潘堂智 锁、多线程
2022 年值得尝试的 7 个 MQTT 客户端工具
关于shell脚本的一些思考
CLIP论文解读
leetcode 剑指 Offer 15. 二进制中1的个数
15年软件架构师经验总结:在ML领域,初学者踩过的五个坑
NAACL 2022 | 具有元重加权的鲁棒自增强命名实体识别技术
Edge box + time series database, technology selection behind Midea's digital platform iBuilding
面试官:为什么 0.1 + 0.2 == 0.300000004?
tidyverse based on data.table?
肝完 Alibaba 这份面试通关宝典,我成功拿下今年第 15 个 Offer
wordpress建立数据库连接时出错