当前位置:网站首页>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 !
边栏推荐
- Priv-app permission异常
- 使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found
- Introduction of pointer variables in function parameters
- Human resource management system based on JSP
- 解决bp中文乱码
- Number of 1 in binary (simple difficulty)
- 【SQL注入点】注入点出现位置、判断
- P35-P41 fourth_ context
- 《牛客刷verilog》Part II Verilog进阶挑战
- The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping
猜你喜欢

Pyqt control part (II)

vulnhub HA: Natraj

使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错

2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test

Php+mysql registration landing page development complete code

使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found

Redis persistence principle

2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination

Asp access teaching management system design finished product

SSM based campus part-time platform for College Students
随机推荐
How to retrieve the password for opening word files
After job hopping at the end of the year, I interviewed more than 30 companies in two weeks and finally landed
stm32逆向入门
Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended
Smart contract security audit company selection analysis and audit report resources download - domestic article
Joint set search: merge intervals and ask whether two numbers are in the same set
I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
GFS分布式文件系统(光是遇见已经很美好了)
【SQL注入点】注入点出现位置、判断
有道云笔记
What's wrong with SD card data damage? How to recover SD card data damage
Design and implementation of JSP logistics center storage information management system
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
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
Wine travel Jianghu War: Ctrip is strong, meituan is strong, and Tiktok is fighting
Kingbasees plug-in KDB of Jincang database_ exists_ expand
Contents of welder (primary) examination and welder (primary) examination in 2022
Preliminary cognition of C language pointer
Introduction of pointer variables in function parameters