当前位置:网站首页>Why is setinterval so easy to get stuck in the high and low level
Why is setinterval so easy to get stuck in the high and low level
2022-06-22 03:26:00 【Lao Zhu Yubing】
Most programmers are setInterval Let's go , If not high concurrency , Program execution is fast , Maybe I don't know what's going on .
Simulate a scene , If we need one client.html , Every second , Back to the server sleep.php Time after processing .
This demand is very simple , Go straight to the code . client.html The code is as follows
<html>
<head>
<meta charset="utf-8">
</head>
<script src="//libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
<body>
<script type="text/javascript">
function test(){
$.get('sleep.php',function(data){console.log(data);});
}
setInterval(test,1000);
</script>
</body>
</html>Server program sleep.php as follows
<?php
echo date('H:i:s');
?>Open with a browser htpt://localhost/client.html , And open the console , The program works well , Perfect .
One day the program was especially popular , The number of users has soared to hundreds of thousands , sleep The program has also become more complex , Add a complex business such as billing system , Lead to sleep.php The execution time is very long , Simulate sleep.php need 3 second . in order to sleep.php good-looking , The old Zhu code is changed to the following .
<?php
while(true){
if(time()%3==0){
echo date('H:i:s');
exit;
}
sleep(1);
}
?>This time refresh http://localhost/client.html , Look at the console , Something amazing happened . Output every three seconds , But the output time becomes 3 individual , Look at the part marked in red . That is to say, the client initiates 3 Two concurrent connections , Reality is sleep.php The longer it takes to execute ,client.html The shorter the interval between requests , Resulting in more concurrency ( Unless the number of browser connections is limited ) . For high concurrency , It's likely to avalanche .

So how to avoid this situation ? How can I really get data from the server every second ? Have a look first interval and setTimeout The difference between! .
setInterval
setInterval() Method can call a function or evaluate an expression according to a specified cycle ( In Milliseconds )
grammar :
setInterval( Function expression , Number of milliseconds );
setInterval() Will keep calling functions , until clearInterval() Called or window closed , from setInterval() Back to ID Value can be used as clearInterval() Method parameters .
setTimeout
setTimeout() Method is used to call a function or evaluate an expression after a specified number of milliseconds ( In Milliseconds )
grammar :
setTimeout( Function expression , Number of milliseconds );
setTimeout() Execute the function only once , If you need multiple calls, you can use setInterval(), Or call again in the function body setTimeout()
difference
From the above analysis, we can see ,setTimeout And setInterval The main difference is :
setTimeout() Method only runs once , In other words, when the set time is reached, start to run the specified code , After running, it's over , If you want to execute the same function again , You can call... Again in the function body setTimeout(), It can achieve the effect of circular call .
setInterval() It is executed in a loop , That is, the corresponding function or expression is executed every time the specified time interval is reached , It's a real timer .
Understand the difference between the two functions ,client.html This program can be changed a little .
<html>
<head>
<meta charset="utf-8">
</head>
<script src="//libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
<body>
<script type="text/javascript">
setTimeout(test,1000);
function test(){
$.get('sleep.php',function(data){
console.log(data);
setTimeout(test,1000);
});
}
</script>
</body>
</html>Refresh the page again , Concurrent disappearance , And on the server sleep.php After execution , Then request the contents of the server .

边栏推荐
- R数据分析:临床预测模型中校准曲线和DCA曲线的意义与做法
- A solution to memory leak in server
- powerdesigner CDM中联系理解
- golang标准库time
- CMD view the console output of hearts, diamonds, spades and clubs to solve the garbled code
- unity3D C# 在区间内生成不重复的随机数
- Implementation of synchronization and atomic operation by mutex mutex in golang concurrent programming
- torch. Max() explanation
- opencv安装(x86/tx2 cuda/共享库)
- tail的用法
猜你喜欢
随机推荐
Are you a technology manager or a project manager?
策略模式
[nvme2.0b 5] sous - système nvm
powerdesigner CDM中联系理解
Explanation of atomic operation in golang concurrent programming
Unity3d C # generates non repeated random numbers in the interval
Splunk: Auto load Balanced TCP Output issue
Figure data platform solution: single node deployment
mysql-索引创建、优化分析、索引优化
华硕重装系统键盘灯失效 =&gt;重装ATK驱动
Using open source software to save an enterprise level map data platform solution
记一则服务器内存泄漏解决过程
cmd看控制台输出红桃、方块、黑桃、梅花乱码解决
策略模式
基于Pytorch的图像分类总结:Swin Transformer
uv_ loop_ Init() process
[nvme2.0b 8] nvme queue arbitration mechanism
【NVMe2.0b 5】NVM Subsystem
Typora + picgo configure the drawing bed to realize automatic uploading of pictures
Will it take three months or half a year to buy financial products in 2022?



![[nvme2.0b 9] controller initialization process](/img/70/536b2c850c611d5d0c013586d4c2d5.png)




