当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
手动输入班级人数及成绩求总成绩和平均成绩?
2022-8-3 第七组 潘堂智 锁、多线程
leetcode 072. Finding Square Roots
leetcode 461. 汉明距离
leetcode 2119. Numbers reversed twice
【使用 Pytorch 实现入门级的人工神经网络】
通关剑指 Offer——剑指 Offer II 009. 乘积小于 K 的子数组
友宏医疗与Actxa签署Pre-M Diabetes TM 战略合作协议
Leetcode 16. Numerical integral power (power + fast recursive/iteration)
Power button 206 - reverse list - the list
抖音web逆向教程
RNA-ATTO 390|RNA-ATTO 425|RNA-ATTO 465|RNA-ATTO 488|RNA-ATTO 495|RNA-ATTO 520近红外荧光染料标记核糖核酸RNA
开源一夏 |如何优化线上服务器
chart.js多条曲线图插件
leetcode 2119. 反转两次的数字
alicloud3搭建wordpress
leetcode 899. 有序队列
leetcode 448. Find All Numbers Disappeared in an Array 找到所有数组中消失的数字(简单)
李沐动手学深度学习V2-自然语言推断与数据集SNLI和代码实现
tRNA-m5C转运RNA(tRNA)修饰5-甲基胞嘧啶(m5C)|tRNA修饰m1Am2A (2-methyladenosine)








