当前位置:网站首页>I stepped on a foundation pit today
I stepped on a foundation pit today
2022-07-03 04:37:00 【Programming samadhi】

introduction
I wonder if you have such an experience : It's wrong to suddenly find out that you have a deep-rooted view for a long time ?
I stepped on a pit today : I always remember in my impression setInterval After definition, it will Execute now Once inside the logic , So when implementing the logic in the figure below , I will take the second step (” Execute the logic immediately a“) It's not necessary , This province has brought me a bug, Khan, ~

It's out bug, We need to remedy , Don't lose your attitude !
Bronze grade
Speaking of the simplest fix , That's the method we often use , The code is as follows :
function fun(){
}
fun();
setInterval(fun, 1000);
Yes , It's so simple and plain !
Diamond grade
Since it's diamond grade , Then you have to play some tricks , Otherwise, it will be useless .
In fact, there is a hidden danger in the above bronze level implementation : It cannot be ensured that there is only one timer executing this logic at the same time . Then let's encapsulate :
let timer = null;
function func(){
}
function mainFun(callback, time){
callback();
return setInterval(callback, time);
}
timer && clearInterval(timer);
timer = mainFun(func, 1000);
Xingyao class
Use the objective function to return the objective function itself .
let timer = null;
function func(){
}
timer && clearInterval(timer);
timer = mainFun(func(), 1000);
Um. , The code style is a little higher .
King level
Since it needs to be executed immediately , Then why not use self executing functions ?
let timer = null;
timer && clearInterval(timer)
timer = setInterval((function func () {
})(), 1000);
Although the logic is the same as above , But I don't know why it's a little tall ,-
Conclusion
In fact, I still don't understand why I have such a wrong memory , And no correction has been found for so long .
It's not terrible to make mistakes , The terrible thing is that you don't realize you've made a mistake !
It takes a good blacksmith to make steel. , Don't look at how big others' implementation methods are , In fact, it is the result of some reasonable combination of basic knowledge , As long as you have a good foundation , Even in the face of a simple piece of code, you can play a different trick !
- ~
- The end of this paper , Thank you for reading !
~
Learn interesting knowledge , Make interesting friends , Create interesting souls !
Hello everyone , I am a 〖 The samadhi of programming 〗 The author of Hermit King , My official account is 『 The samadhi of programming 』, Welcome to your attention , I hope you can give me more advice !
边栏推荐
- Bugku CTF daily question baby_ flag. txt
- FISCO bcos zero knowledge proof Fiat Shamir instance source code
- [set theory] ordered pair (ordered pair | ordered triple | ordered n ancestor)
- Drf--- quick start 01
- RSRS指标择时及大小盘轮动
- 智能合约安全审计公司选型分析和审计报告资源下载---国内篇
- Jincang KFS data bidirectional synchronization scenario deployment
- 逆袭大学生的职业规划
- 解决bp中文乱码
- Web - Information Collection
猜你喜欢

2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
![[dynamic programming] subsequence problem](/img/d8/020ae959ef53ce097d3a81a0d2d63a.jpg)
[dynamic programming] subsequence problem

When using the benchmarksql tool to test the concurrency of kingbasees, there are sub threads that are not closed in time after the main process is killed successfully

Arthas watch grabs a field / attribute of the input parameter

Games101 Lesson 9 shading 3 Notes

Which Bluetooth headset is cost-effective? Four Bluetooth headsets with high cost performance are recommended

The least operation of leetcode simple problem makes the array increment

解决bp中文乱码

Number of uniform strings of leetcode simple problem

Introduction of pointer variables in function parameters
随机推荐
Introduction of pointer variables in function parameters
带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
How to use kotlin to improve productivity: kotlin tips
Small sample target detection network with attention RPN and multi relationship detector (provide source code, data and download)
How to retrieve the password for opening word files
What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets
2022 P cylinder filling test content and P cylinder filling simulation test questions
金仓数据库KingbaseES 插件kdb_exists_expand
2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical
逆袭大学生的职业规划
Reptile exercise 02
[Thesis Writing] how to write the overall design of JSP tourism network
2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis
How do you use lodash linking function- How do you chain functions using lodash?
有道云笔记
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
When using the benchmarksql tool to test the concurrency of kingbasees, there are sub threads that are not closed in time after the main process is killed successfully
Triangular rasterization
Leetcode simple question: check whether the string is an array prefix
[dynamic programming] subsequence problem